如何获取第一个和第二个对象

时间:2015-04-02 22:18:03

标签: ios parse-platform

我想知道如何获得一个类的第一个和第二个对象。我为第一个目标实现了它:

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

从这里开始,您建议如何获得下一个对象?

由于

2 个答案:

答案 0 :(得分:3)

使用findObjects获取匹配对象的数组。设置最大计数以返回.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);
    }
}];

答案 1 :(得分:0)

您只需添加query.limit=2;即可。这是完整的代码:

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