Objective c CloudKit查询时间

Objective c CloudKit查询时间,objective-c,cloudkit,Objective C,Cloudkit,我刚刚开始试用CloudKit,查询速度非常慢。以下是我正在使用的一些示例代码: //CLOUDKIT CKContainer *container = [CKContainer defaultContainer]; CKDatabase *privateDatabase = [container privateCloudDatabase]; NSPredicate *predicate = [NSPredicate predicateWithFormat:@&

我刚刚开始试用CloudKit,查询速度非常慢。以下是我正在使用的一些示例代码:

//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);
        }
        
    }];
我获取私有数据库,并查询所有记录。我在仪表板中添加了四个简单的选项

调用此代码后,我可以从控制台日志中看到,几乎立即调用成功消息,结果数组为null。稍后,返回结果,如日志中所示。但是,警报视图不会显示,结果会在我的表格中显示大约3-4秒


发生了什么事?

这已经解决了。正如Edwin提到的,我不知道回调在后台线程上。因此,当我在完成块中调用
[self.tableView reloadData]
时,它也在后台线程上运行

通过将其放回主线程,表视图将在大约一秒钟内重新加载。如果在与回调相同的线程上运行,则大约需要4-5秒

    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^{
        dispatch_async(dispatch_get_main_queue(), ^{
                           [self.tableView reloadData];
                       });
    });

如果我误解了,请告诉我,但我认为这就是问题所在。

在将结果分配给它之前,您正在记录fetchedRecords。此回调也在后台线程上调用。您应该从主线程调用allertview。哎呀!但是如果我不能在完成块中调用我的“完成”代码,那么竞争块的意义何在?我通过将tableview的
reloadData
放回主线程来解决这个问题。查看我的答案。我使用了:dispatch_async(dispatch_get_main_queue(),^{[thisAlert show];});接下来我要做的就是发出警报。