1

Can anyone show some sample on how to do the pagination with AFPaginator. I am using AFRestClient and I need to associate pagerId, offset and count parameters in my request for the entity.

It looks to me that by using AFPaginator associating these parameters should be straight forward. I have searched around for some examples and couldn't find any.

jaishankar
  • 249
  • 1
  • 4
  • 13

1 Answers1

0

I could make some progress with pagination, but not completed.

If the fetch request has fetchLimit and fetchOffset not equal zero, it will create the parameters on request.

So, on my AFRESTClient subclass, before creating the request, on the method -requestForFetchRequest:withContext: I set the paginator as:

NSMutableDictionary *mutableParameters = [NSMutableDictionary dictionary];
self.paginator = [AFPageAndPerPagePaginator paginatorWithPageParameter:@"page" perPageParameter:@"per"];
[mutableParameters addEntriesFromDictionary:[self.paginator parametersForFetchRequest:fetchRequest]];
request = [self requestWithMethod:@"GET" path:@"feed" parameters:mutableParameters];

This will generate a request for something like http://domain.com/feed?page=1&per=20. The page is calculated based on fetchOffset and FetchLimit as

NSUInteger perPage = fetchRequest.fetchLimit == 0 ? kAFPaginationDefaultPerPage : fetchRequest.fetchLimit;
NSUInteger page = fetchRequest.fetchOffset == 0 ? kAFPaginationDefaultPage : (NSUInteger)floorf((float)fetchRequest.fetchOffset / (float)perPage) + 1;

So, I could make the fetch ask for a different page, but as I'm using a NSFetchedResultsController I couldn't make it ask for another page when I scroll a UITableView. I tried to do something as:

[NSFetchedResultsController deleteCacheWithName:[self.fetchedResultsController cacheName]];
self.fetchedResultsController.fetchRequest.fetchOffset = self.visibleChunk * self.chunkSize;
[_fetchedResultsController performSelectorOnMainThread:@selector(performFetch:) withObject:nil waitUntilDone:NO modes:@[ NSRunLoopCommonModes ]];

Here, visibleChunk should be used to bring another page, and chunkSize is the number of desired itens one API request.

This is not direct a response for your question, but I hope it can help you find the answer and post here :)

If I find something more, I update this.

  • I managed to make it work. But it does not work with NSFetchedResultsController. With NSFetchRequest, you just have to set `fetchOffset` with multiples of `fetchLimit`. So, if yo want to get the page 2 for 10 items per page, set `fetchOffset` equal 20, execute a new fetch with this changed NSFetchRequest object and you are done. – Tales Pinheiro De Andrade Sep 02 '13 at 16:45