Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CloudKit: creatorUserRecordID of CKRecord Issue (or Bug?)

Tags:

ios

cloudkit

After iOS 8.3, if the record is created by the current account, its creatorUserRecordID will be like

CKRecordID: [Some Address]; defaultOwner:(_defaultZone:defaultOwner)

And then if fetch this recordID using fetchRecordWithID:completionHandler: from CKDatabase, it will always return error like

CKError [Some Address]: "Unknown Item" (11/2003); server message = "Record not found"; uuid = [Some UUID]; container ID = [Some Container ID]

I never encounter this issue before.

Is it a bug, or should I fetch record from recordID like this ( defaultOwner ) in other way?

EDIT (add sample code)

- (void)postMoodFeed:(NSString *)moodFeed
{
    CKRecord *moodRecord = [[CKRecord alloc] initWithRecordType:@"Mood"];
    moodRecord[@"moodFeed"] = moodFeed

    [[[CKContainer defaultContainer] publicCloudDatabase] saveRecord:moodRecord completionHandler:^(CKRecord *record, NSError *error) {
        [self queryMyMood];
    }];
}

- (void)queryMyMood
{
    // currentUserRecordID is fetched from fetchUserRecordIDWithCompletionHandler: of CKContainer
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"creatorUserRecordID = %@", currentUserRecordID];

    CKQuery *query = [[CKQuery alloc] initWithRecordType:@"Mood" predicate:predicate];

    [[[CKContainer defaultContainer] publicCloudDatabase] performQuery:query inZoneWithID:nil completionHandler:^(NSArray *results, NSError *error) {
        if (results) {
            for (CKRecord *eachRecord in results) {
                // Following logs are all __defaultOwner__
                NSLog(@"%@", eachRecord.creatorUserRecordID.recordName);
                [[[CKContainer defaultContainer] publicCloudDatabase]fetchRecordWithID:eachRecord.creatorUserRecordID completionHandler:^(CKRecord *record, NSError *error) {
                    // All following logs are "Unknown item" error
                    NSLog(@"%@", error);
                }];
            }
        }
    }];
}

EDIT July 2, 2015

That is a bug.

After reporting to Apple, they fixed this issue in iOS 9 Beta 2.

like image 657
ananfang Avatar asked Dec 06 '25 07:12

ananfang


1 Answers

Indeed it looks like new functionality.

What you could do is first testing if the eachRecord.creatorUserRecordID.recordName == "defaultOwner" and if that's the case you could fetch the record for the ID that you got from the currentUserRecordID

But it would be better to not use the creatorUserRecordID for any functionality in your app. You could better add a new CKReference field and always fill it with the currentUserRecordID. Then even if you have a process that migrates data, you would still know who created that record originally.

like image 97
Edwin Vermeer Avatar answered Dec 08 '25 20:12

Edwin Vermeer



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!