Paginate parsing requests using regular UIViewController
I have UIViewController
a table view. I am populating a tableview using normal methods. I am calling my request using the method loadEntries
.
The problem is that the number of records loadEntries
can be more than tens of thousands.
I would prefer not to change my ViewController to PFQueryTableViewController, an exception can be thrown if there is a hidden way to change UIViewcontroller to PFQueryTableViewController.
so my questions are: is it possible to implement pagination using parse.com queries (not using PFQueryTableViewController), if so how?
source to share
You need to look at the parameter skip
PFQuery
as discussed here in the Parse PFQuery documentation .
You can run the query the first time as just a counter, and then you can determine how many data pages you will have.
You can then run a query with skip
and 'count' values based on the "page" the user is currently viewing.
Something like that:
- (void)countRecords {
PFQuery *query = [PFQuery queryWithClassName:@"className"];
// Other query parameters assigned here...
[query countObjectsInBackgroundWithBlock:^(int count, NSError *error) {
// Do better error handling in your app...
self.recordCount = count;
self.pageCount = count / self.recordsPerPage + 1;
[self loadRecordsForPageIndex:0 countPerPage:self.recordsPerPage];
}];
}
- (void)loadRecordsForPageIndex:(NSInteger)pageIndex countPerPage:(NSInteger)count {
PFQuery *query = [PFQuery queryWithClassName:@"className"];
// Other query parameters assigned here...
query.limit = count;
query.skip = pageIndex * count;
[query findObjects... // You should know this part
}
The above example -countRecords
gets the current number of records that match your query and then automatically calls -loadRecordsForPageIndex:countPerPage:
. Later, when the user navigates between the data pages, you call this again, passing the new values pageIndex
and count
(writes to the page). You can refactor this to refer to self.recordsPerPage
as an instance variable or not.
From Parse.com directly:
Caveat: the number of requests is limited to 160 requests per minute. They can also return inaccurate results for classes with more than 1000 objects. Thus, it is preferable to archive the application to avoid this kind of counting operation (eg using counters).
MY SEALS:
- The default for
limit
is 100. - The maximum allowed value for
limit
is 1000. - The default value for
skip
is 0 (zero). - The maximum allowed value for
skip
is 10,000. - Therefore, you cannot reliably query more than 11,000 objects with a single, unmodified query. (You can get advanced search and search with
createdAt
or whatever, but these will bePFQuery
instances with different restrictions.)
source to share