The data browser is built for Chrome, Firefox, Safari, and IE9 and above. Please upgrade your browser, or download Google Chrome to get the best experience.
Back to Questions

Fetch all data in a table using PFQuery

4 votes     4 answers     15k views     

4

i have one class Name tempClass if i want to find all data in to it how i can do with the use of the PFQuery? any one plz explain with code?

4 Answers

11

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]);
  }
}];
3

thanks hactor, i solve the problem by seting read true for all user.

0

still i am not able to get all the data,let me explain all situation,

i have one userclass and another tempClass. in tempClass i have one column name "Parent" which store userclass ID in it.

i created tempClass with following code,

PFUser *user = [PFUser currentUser];
PFObject *myComment = [PFObject objectWithClassName:@"tempClass"];
[myComment setObject:lblValEname.text forKey:@"e_name"];
[myComment setObject:user forKey:@"parent"];
[myComment save];

So, when i login with any of user lets say "Test1",which have 2 entries related to the tempClass and if i fire query

PFQuery query = [PFQuery queryWithClassName:@"tempClass"]; [query findObjectsInBackgroundWithBlock:(NSArrayobjects, 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]);

} }];

i got only 2 object as the result instead of 4 (total object in tempClass). so plz give me soultion with code and explain me why i get 2 object?

Your tempClass objects have various ACLs that keep them from being readable by all users. Your query will only return objects that have read: true set for the logged in user.

- Héctor Ramos over 1 year ago
-1

This might be a dumb question but how can you keep the query getting all objects without adding this bit in each block:

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.

}

It is not supported behavior to get large numbers of results.

- Fosco Marotto about 1 year ago