Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is CloudKit strongly consistent, or eventually consistent?

Do the official docs talk somewhere about CloudKit consistency? According to my tests it appears to be eventually consistent – reading a record immediately after writing it might work and might not (returning empty results):

CKDatabase *database = [[CKContainer defaultContainer] publicCloudDatabase];
CKRecord *record = [[CKRecord alloc] initWithRecordType:@"Foo"];

dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);
[database saveRecord:record completionHandler:^(CKRecord *record, NSError *error) {
    CKQuery *query = [[CKQuery alloc] initWithRecordType:@"Foo" predicate:[NSPredicate predicateWithFormat:@"TRUEPREDICATE"]];
    [database performQuery:query inZoneWithID:nil completionHandler:^(NSArray *results, NSError *error) {
        XCTAssertEqualObjects(results, @[], @"Freshly written object not returned by query."); // succeeds
        dispatch_semaphore_signal(semaphore);
    }];
}];

dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);

Is there a way to force a strongly consistent read that would reflect all previous updates?

like image 448
zoul Avatar asked May 22 '26 22:05

zoul


1 Answers

It's a little of both: CloudKit is strongly consistent if you fetch a record by identifier, but eventually consistent when you fetch a record with a query.

When a CKModifyRecordsOperation returns successfully the record is immediately fetchable by its identifier.

However, it takes some time for the server to scan the record's values and update and distribute its search indexes. Until that indexing completes you won't see the record in any queries.

like image 80
farktronix Avatar answered May 27 '26 11:05

farktronix