Restkit 0.2 supports pagination through a class called RKPaginator. It has all the necessary methods for supporting pagination properly and efficiently.
The first step is to define the properties required for pagination.
1234
//Properties required for pagination@property(nonatomic,strong)RKPaginator*paginator;@property(nonatomic,strong)NSMutableArray*objects;@property(nonatomic,assign)BOOLisPaginatorLoading;
Also make sure to define the url path for your request. The parameters after : denotes the attributes of the RKPaginator object. For e.g, in the below path, instead of :currentPage, the value that will be passed is the currentPage attribute of the paginator object. The two attributes that are necessarily required are perPage and currentPage.
// Create weak reference to self to use within the paginators completion block__weaktypeof(self)weakSelf=self;weakSelf.objects=[NSMutableArrayarray];// Setup paginatorif(!self.paginator){RKObjectManager*objectManager=[RKObjectManagersharedManager];NSString*requestString=kUrlStringForPagination;self.paginator=[objectManagerpaginatorWithPathPattern:requestString];self.paginator.perPage=10;//Show loader //Code to show loader //Set completion block for this paginator[self.paginatorsetCompletionBlockWithSuccess:^(RKPaginator*paginator,NSArray*objects,NSUIntegerpage){weakSelf.isPaginatorLoading=NO;//Hide Loader //Code to hide Loaderif(page==1){[weakSelf.objectsremoveAllObjects];}[weakSelf.objectsaddObjectsFromArray:objects];NSLog(@"Loaded Objects are %@",weakSelf.objects);}failure:^(RKPaginator*paginator,NSError*error){[MBProgressHUDhideHUDForView:weakSelf.viewanimated:YES];weakSelf.paginator=nil;}];}NSLog(@"Loaded Objects are %@",weakSelf.objects);[self.paginatorloadPage:1];
Now, put this code whenever you are interested to load the next page.
1234567
if([self.paginatorhasNextPage]){NSLog(@"Loading next page");if(!self.isPaginatorLoading){self.isPaginatorLoading=YES;[self.paginatorloadNextPage];}}
Also, make sure that the response descriptor is set for this call.