rushelmet
rushelmet

Reputation: 305

How to get the first and second objects

I would like to know how can I get the first and second objects of a class. I achieved it for the first object :

PFQuery *query = [PFQuery queryWithClassName:@"YourClassName"];
[query orderByDescending:@"createdAt"];
[query getFirstObjectInBackgroundWithBlock:^(PFObject *object, NSError *error) {
    // code
}];

From here, what do you suggest to get the next object ?

Thanks

Upvotes: 0

Views: 167

Answers (2)

danh
danh

Reputation: 62676

Use findObjects to get an array of matching objects. Set a max count to return with .limit:

PFQuery *query = [PFQuery queryWithClassName:@"YourClassName"];
[query orderByDescending:@"createdAt"];
query.limit = 2;
[query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
    for (PFObject *object in objects) {
        // this will run up to 2 times
        NSLog("%@", object);
    }
}];

Upvotes: 3

Gibran
Gibran

Reputation: 934

you just have to add query.limit=2;. Here's the whole code :

PFQuery *query = [PFQuery queryWithClassName:@"YourClassName"];
[query orderByDescending:@"createdAt"];
query.limit = 2;
[query getFirstObjectInBackgroundWithBlock:^(PFObject *object, NSError *error) {
    // code
}];

Upvotes: 0

Related Questions