Ramesh Ramesh - 5 months ago 18
iOS Question

How to Fetch all data in a table using PFQuery in iOS?

i am newbie here in iOS and Parse Framework i want to fetch data from my Parse table from PFQuery like as

NSUInteger limit = 1500;
PFQuery *query = [PFQuery queryWithClassName:@"MapInfo"];
[query setLimit: limit];
[query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error)
{
if (!error) {
NSLog(@"Successfully retrieved: %@", objects);
} else {
NSString *errorString = [[error userInfo] objectForKey:@"error"];
NSLog(@"Error: %@", errorString);
}
}];


it is Working as i want but it is give me only 1000 objects i want here to fetch all my table data it contain unto 2000 object and it will increment as day by day please help me for this.

For this Now i write a code like this but it is Only Give me 1000 Objects only

NSMutableArray *allObjects = [NSMutableArray array];
NSUInteger limit = 0;
__block NSUInteger skip = 0;
PFQuery *query = [PFQuery queryWithClassName:@"MapInfo"];
[query setLimit: limit];
[query setSkip: skip];
[query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
if (!error) {
[allObjects addObjectsFromArray:objects];
if (objects.count == limit) {

skip += limit;
[query setSkip: skip];
[query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
NSLog(@"Array %@",objects);
}];
}

} else {
NSLog(@"Error: %@ %@", error, [error userInfo]);
}
}];


thanks.

Answer

https://parse.com/questions/fetch-all-data-in-a-table-using-pfquery

You can use the skip and limit parameter to paginate through all objects in the table by adding the value of limit to skip until the query returns an amount of objects that is less than limit.

NSMutableArray *allObjects = [NSMutableArray array];
NSUInteger limit = 0;
__block NSUInteger skip = 0;
PFQuery *query = [PFQuery queryWithClassName:@"MapInfo"];
[query setLimit: limit];
[query setSkip: skip];
[query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
  if (!error) {
    // The find succeeded. Add the returned objects to allObjects
    [allObjects addObjectsFromArray:objects];
    if (objects.count == limit) {
      // There might be more objects in the table. Update the skip value and execute the query again.
      skip += limit;
      [query setSkip: skip];
      [query findObjects... // Execute the query until all objects have been returned. Keep adding the results to the allObjects mutable array.

    }
  } else {
    // Log details of the failure
    NSLog(@"Error: %@ %@", error, [error userInfo]);
  }
}];