CloudKit查询时间 [英] CloudKit Query Time
问题描述
我刚刚开始试用CloudKit,查询时间有些慢。这是我正在使用的一些示例代码:
I've just started trialling CloudKit and am having some pretty slow query times. Here is some sample code I am using:
//CLOUDKIT
CKContainer *container = [CKContainer defaultContainer];
CKDatabase *privateDatabase = [container privateCloudDatabase];
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"TRUEPREDICATE"];
CKQuery *query = [[CKQuery alloc] initWithRecordType:@"FlightLog" predicate:predicate];
[privateDatabase performQuery:query inZoneWithID:nil completionHandler:^(NSArray *results, NSError *error) {
//SUCCESS
if (!error)
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"SUCCESS" message:@"IT WORKED" delegate:self cancelButtonTitle:@"dismiss" otherButtonTitles:nil];
[alert show];
NSLog(@"%@", @"fetchFlights success!");
NSLog(@"%@", self.fetchedRecords);
self.fetchedRecords = results;
[self.tableView reloadData];
}
//ERROR
else
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"error" message:error.localizedDescription delegate:self cancelButtonTitle:@"dismiss" otherButtonTitles:nil];
[alert show];
NSLog(@"%@", error);
}
}];
我得到了私有数据库,并查询了所有记录。在仪表板中仅添加了四个简单的代码。
I get the private database, and query for all records. There is just four simple ones I added in the dashboard.
调用此代码后,我可以从控制台日志中看到成功消息几乎立即被调用,并带有一个结果数组为空。然后,稍后返回结果,如日志中所示。但是,没有显示警报视图,并且结果在我的表格中显示了大约3-4秒。
Upon calling this code, I can see from my console log that the success message gets called almost immediately, with a null results array. Then moments later, the results are returned, as seen in the log. However, the alert view isn't shown and results displayed in my table for about 3-4 more seconds.
发生了什么事!!
谢谢。
推荐答案
已解决。正如Edwin提到的,我不知道回调是在后台线程上进行的。因此,当我在完成块中调用 [self.tableView reloadData]
时,它也在后台线程上运行。
This is resolved. As Edwin mentions, I didn't know that the callback is on a background thread. So when I call [self.tableView reloadData]
in the completion block, it is also running on the background thread.
将其返回主线程后,表视图将在大约一秒钟内重新加载。如果在与回调相同的线程上运行,则耗时约4-5秒。
By putting it back on the main thread, the table view reloads within about a second. Vs taking about 4-5 seconds if running on the same thread as the callback.
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^{
dispatch_async(dispatch_get_main_queue(), ^{
[self.tableView reloadData];
});
});
让我知道我是否被误解了,但我认为这就是发生的事情。
Let me know if I have misunderstood, but I think that's what has happened.
这篇关于CloudKit查询时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!