在Parse中获取任意大量的对象

时间:2014-10-02 22:44:53

标签: ios parse-platform

在我的Parse数据库中,我有一个" Connection"加入两个用户的类。在我的一种方法中,我试图获取一个人连接的所有用户。但是,我被困在两个不同的选项之间,这两个选项似乎都没有效率。

我的第一反应是不断查询数据库直到返回所有对象(例如"查询前1000,查询后1000,...")。在大多数情况下,这将是理想的,因为大多数用户将拥有少于1000个连接(可能更像是2-300左右)。但是,如果某些用户拥有疯狂的连接数量,那么这项工作确实无法正常工作 - 特别是因为Parse已经突破限制。

另一个选项似乎是使用Query.each方法,简单地迭代匹配查询的所有记录。我相信无论元素数量多少都会有效,所以这很好。然而,听起来这对于大尺寸来说相对较慢并且可能会超时。

那么,在Parse的限制下,最好的方法是什么?我希望它在相对较少数量的对象的常见情况下快速,但它肯定不会因边缘情况而中断。当然,一种选择是不进行此类查询,但在客户端进行所有连接非常有用。谢谢!

1 个答案:

答案 0 :(得分:5)

很好的问题,而且我已经为自己的工作找到了一个很好的解决方案。最快,最有效的解决方法是查询对象计数,如果超过1000,则在同一个表上运行多个查询,每次查询的skip属性增加1000:

通过[query countObjectsInBackgroundWithBlock]获取记录总数,并使用它来设置'skip'变量。为每1000条记录运行一个新查询,相应地更新skip属性 每个查询返回时正常处理记录。

您还需要一个帮助方法来确定所有查询何时完成,并且您已准备好在本地处理完整的数据集。

这样的事情:

//Define these as class-wide variables in your @interface - don't forget to @synthesize them too!
int queriesToComplete;
int completedQueries;
NSMutableArray *completeDataArray = [[NSMutableArray alloc] init];

//Put in your query load function
PFQuery *recordsCountQuery = [PFQuery queryWithClassName:@"YourClassName"];
[recordsCountQuery countObjectsInBackgroundWithBlock:^(int number, NSError *error) {
    //Determine how many queries to run
    queriesToComplete = (int)ceil(number/1000) + 1;
    //Set the number of completed queries to zero
    completedQueries = 0;
    //Run a chain of queries - be sure to run them in the background to prevent UI lockup
    for (int i = 0; i < queriesToComplete; i++) {
        PFQuery *chainQuery = [PFQuery queryWithClassName:@"YourClassName"];
        //Add your other 'whereKey: equalTo:' type qualifiers here
        chainQuery.limit = 1000;
        chainQuery.skip = (i*1000);
        [chainQuery findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
            //NSLog(@"query done");
            [completeDataArray addObjectsFromArray:[objects copy]];
            completedQueries ++;
            [self pullCompletionCheck];
        }];
    }
}];

- (void)pullCompletionCheck{
    if (self.queriesToComplete == self.completedQueries) {
        //All queries completed.  
        //All data is now in the completeDataArray

    }
}
相关问题