3

I’m allowing Users to save locations. It is saving in cloudKit, I can fetch all the locations successfully. For each location saved there is a record who created it "created by: IDXXX". How do I pull all the locations saved by the single user who created it?

In CloudKit I have 2 record types, Location and Users. When a location is saved it is saved as a Location and I add a reference to the User’s record ID using CKReference to a field in Location that I have marked as “owningList” for attribute name with attribute type “Reference”. This method below is being called when a new location is saved grabbing the new saved location's CKRecord.

- (void)saveLocationToUserWithLocationRecord:(CKRecord *)locRecord {

    CKReference *ref  = [[CKReference alloc] initWithRecordID:self.userRecordID action:CKReferenceActionDeleteSelf];

    [locRecord setObject:ref forKey:@"owningList"];

}

Then I try to fetch all the locations with predicate specifying the CKReference which is the user’s Record ID and the field name “owningList” and this returns no result. I tried other ways without success. I’m very new to this any help or clarification would be greatly appreciated.

-(void)fetchUsersSavedLocations:(CKRecordID *)ID {

    CKDatabase *database = [[CKContainer defaultContainer] publicCloudDatabase];

    CKReference* recordToMatch = [[CKReference alloc] initWithRecordID:ID

                                                                action:CKReferenceActionDeleteSelf];

    NSPredicate* predicate = [NSPredicate predicateWithFormat:@"%K == %@", @"owningList", recordToMatch];

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

    [database performQuery:query inZoneWithID:nil completionHandler:^(NSArray *results, NSError *error) {

        if (!error) {
            NSLog(@"SUCCESS results %@”, results);
        }
        else {
            NSLog(@"failed with error %@", error);
        }

    }];

}
julien
  • 437
  • 4
  • 9
  • 1
    I was able to resolve what I wanted to accomplish by simply querying all the locations with this Predicate: `NSPredicate *predicate = [NSPredicate predicateWithFormat:@"creatorUserRecordID == %@", self.userRecordID];` – julien Apr 07 '15 at 06:25
  • 1
    Your code looks OK. The only think I can think of is that you save a different id to the owningList field than what you query. Have you looked in the CloudKit dashboard to see if the ID's match? – Edwin Vermeer Apr 07 '15 at 06:57
  • When using the creatorUserRecordID you have to be aware that if you want to migrate your data in the future, and you created a process to do that, that the ID will be set to the ID of the process that migrates the data. – Edwin Vermeer Apr 07 '15 at 06:58
  • Thanks for that info. In CloudKit the owningList for every location saved doesn't have a value. The reference which is the user's record ID should be in that field. So I wonder why this is. – julien Apr 07 '15 at 13:57

0 Answers0