CloudKit Query Time

我们两清 提交于 2019-12-24 11:28:37

问题


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.

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.

Whats going on?!

Thanks.


回答1:


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, its is also running on the background thread.

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 I have misunderstood, but I think thats what has happened.



来源:https://stackoverflow.com/questions/26257952/cloudkit-query-time

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!