gpt4 book ai didi

ios - 在 Parse 中获取任意数量的对象

转载 作者:可可西里 更新时间:2023-11-01 05:34:13 26 4
gpt4 key购买 nike

在我的 Parse 数据库中,我有一个连接两个用户的“Connection”类。在我的一种方法中,我试图获取个人连接到的所有用户。然而,我被困在两个不同的选项之间,这两个选项似乎都不是很有效。

我的第一 react 是不断查询数据库,直到返回所有对象(例如“查询第一个 1000,查询第二个 1000,...”)。在大多数情况下,这将是理想的,因为大多数用户将拥有少于 1000 个连接(可能更像是 2-300 左右)。但是,如果某些用户的连接数量过多,这将不会很好地工作 - 特别是因为 Parse 有突发限制。

另一种选择似乎是使用 Query.each 方法,简单地遍历与查询匹配的所有记录。我相信无论元素的数量如何,这都会起作用,所以这很好。但是,听起来这对于大尺寸来说相对较慢并且可能会超时。

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

最佳答案

很好的问题,我已经为自己的工作找到了一个很好的解决方案。最快、最有效的解决方法是查询对象计数,如果超过 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

}
}

关于ios - 在 Parse 中获取任意数量的对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26170814/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com