Skip to content

Instantly share code, notes, and snippets.

@matzew
Forked from secondsun/paging-usage-comparison.md
Last active December 11, 2015 04:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save matzew/4546737 to your computer and use it in GitHub Desktop.
Save matzew/4546737 to your computer and use it in GitHub Desktop.

Below is a comparison of the method usage proposed by each lib for handling paged resources.

Read Next Page

Android

ReadFilter filter = new ReadFilter();
filter.setLimit(5);

cars.readWithFilter(filter, new Callback<Car>() {
  @Override
  void onSuccess(List<Car> data) {
    firstPage = data;
  }
  
  @Override
  void onError(Exception ex) {
    //handle error
  }
});


firstPage.next(new CallBack<Car>() {
  @Override
  void onSuccess(List<Car> secondPagedList) {
    //Do somethign with second Paged list
  }
  
  @Override
  void onError(Exception ex) {
    //handle error
  }
});

iOS

// the AGPagedList category:
__block NSArray *_pagedList;

// start the paging........
[pipe readWithFilter:^(id<AGFilterConfig> config) {
    //set up the paging details:
    [config setLimit:3];
    [config setOffset:0];
} success:^(id responseObject) {
    // stash the "paged/query result" object (or what ever the name will be)
    _pagedList = responseObject;

    // show the results - somehwre
    NSLog(@"RESULT, PAGE 1: %@", listOfObjects);
} failure:^(NSError *error) {...}];

// get the second page.....
[_pagedList next:^(id responseObject) {
    // show the results - somehwre
    NSLog(@"RESULT, PAGE 2: %@", responseObject);
} failure:^(NSError *error) {...}];

JS

cars.read({
    page: "next",
    success: function( data ) {
        // do something
    },
    error: function() {
        // handle it
    }
});

Read Previous Page

Android

ReadFilter filter = new ReadFilter();
filter.setLimit(5);
filter.setOffset(1);
cars.readWithFilter(filter, new Callback<Car>() {
  @Override
  void onSuccess(List<Car> data) {
    secondPage = data;
  }
  
  @Override
  void onError(Exception ex) {
    //handle error
  }
});

secondPage.prev(new CallBack<Car>() {
  @Override
  void onSuccess(List<Car> firstPagedList) {
    //Do somethign with second Paged list
  }
  
  @Override
  void onError(Exception ex) {
    //handle error
  }
});

iOS

// go backwards....,
[_pagedList previous:^(id responseObject) {
    /// do something with the payload
} failure:^(NSError *error) {...}];

JS

cars.read({
    page: "prev",
    success: function( data ) {
        // do something
    },
    error: function() {
        // handle it
    }
});

Change Offset and Limit

###Android

ReadFilter filter = new ReadFilter();
filter.setLimit(5);
filter.setOffset(1);

cars.readWithFilter(filter, new Callback<Car>() {
  @Override
  void onSuccess(List<Car> data) {
    //do something
  }
  
  @Override
  void onError(Exception ex) {
    //handle error
  }
});


filter = new ReadFilter();
filter.setLimit(50);
filter.setOffset(0);

cars.readWithFilter(filter, new Callback<Car>() {
  @Override
  void onSuccess(List<Car> data) {
    //do something
  }
  
  @Override
  void onError(Exception ex) {
    //handle error
  }
});

###iOS

// REDEFINE the query/pagination, and start over:
[pipe readWithFilter:^(id<AGFilterConfig> config) {
    // now we want 10 per page....
    [config setLimit:10];
  // starting at page 1.........
    [config setOffset:1];
} success:^(id responseObject) {
    // stash the "paged/query result" object (or what ever the name will be)
    _pagedList = responseObject;

    // show the results - somehwre
        NSLog(@"RESULT, PAGE 1: %@", listOfObjects);
} failure:^(NSError *error) {...}];

###JS

cars.updatePageConfig({
    offset: 2,
    limit: 10
});

Return All Records (No longer paged)

Android

cars.read(new Callback<Car>() {
  @Override
  void onSuccess(List<Car> allRecords) {
   //do something 
  }
  
  @Override
  void onError(Exception ex) {
    //handle error
  }
});

iOS

[pipe read:^(id responseObject) {
    // do something with the response...
} failure:^(NSError *error) {...}];

JS

// Get all records for a single read but continue paging aftwerward
cars.read({
    page: false,
    success: function( data ) {
        // do something
    },
    error: function() {
        // handle it
    }
});

// Or permanently update the config to no longer page the results
cars.updatePageConfig({
    paged: false
});

cars.read({
    success: function( data ) {
        // do something
    },
    error: function() {
        // handle it
    }
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment