You're looking at our questions archive. This question was last updated about 1 month ago.

If you're looking for help, head over to the Parse Developers Google Group or check out the parse.com tag on Stack Overflow.

Help Google Group Stack Overflow

Fetch all data in a table using PFQuery

6 votes 1 answer 46.8k views about 1 month ago

I have one class, named TempClass. If I want to query all data within it, how can I do so with the use of a PFQuery? Can anyone provide some example code?

1 Answer

You can use a simple query with no constraints to obtain up to 100 objects from your class.

PFQuery *query = [PFQuery queryWithClassName:@"tempClass"];
[query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
  if (!error) {
    // The find succeeded. The first 100 objects are available in objects
  } else {
    // Log details of the failure
    NSLog(@"Error: %@ %@", error, [error userInfo]);
  }
}];

The default limit is 100, but it can be changed up to 1,000 by setting the limit parameter:

PFQuery *query = [PFQuery queryWithClassName:@"tempClass"];
[query setLimit: 1000];
[query findObjectsInBackground:...

This query will return the first 1,000 objects in the class (indexes 0 through 999). You can use a skip parameter to skip a given amount of objects. If you want to get the next 1,000 objects in the class, you would use a skip value of 1,000 to skip over the first 1,000 results. This query would return the objects at indexes 1,000 through 1,999.

PFQuery *query = [PFQuery queryWithClassName:@"tempClass"];
[query setLimit: 1000];
[query setSkip: 1000];
[query findObjectsInBackground:...

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;
NSUInteger skip = 0;
PFQuery *query = [PFQuery queryWithClassName:@"tempClass"];
[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]);
  }
}];

You can also use Parse.Query.each() in the JavaScript SDK (inside a Background Job or Node.js app, for example) to automatically iterate through every single object that matches the query with no limits.