ananfang
ananfang

Reputation: 566

CloudKit: creatorUserRecordID of CKRecord Issue (or Bug?)

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.

Upvotes: 3

Views: 2034

Answers (2)

helpful hint
helpful hint

Reputation: 1

Maybe this will help: I've found that if you use [CKFetchRecordsOperation fetchCurrentUserRecordOperation] at the beginning of your app's workflow you won't have defaultOwner CKRecordIDs come out of nowhere.

Upvotes: 0

Edwin Vermeer
Edwin Vermeer

Reputation: 13127

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.

Upvotes: 1

Related Questions