How do I fetch all CloudKit Records created by the current user?

Say I have a 'Species' Record Type that contains Public Records which were created by a number of Users. Currently, my query retrieves all records of 'Species':


private func fetchSpecies() {
        // Fetch Public Database
        let publicDatabase = CKContainer.defaultContainer().publicCloudDatabase
        
        // Initialize Query
        let query = CKQuery(recordType: "Species", predicate: NSPredicate(format: "TRUEPREDICATE"))
        
        // Configure Query
        query.sortDescriptors = [NSSortDescriptor(key: "latinName", ascending: true)]
        
        // Perform Query
        publicDatabase.performQuery(query, inZoneWithID: nil) { (records, error) -> Void in
            dispatch_async(dispatch_get_main_queue(), { () -> Void in
                // Process Response on Main Thread
                self.processResponseForQuery(records, error: error)
            })
        }
    }

How can I only fetch records that were created by the current user (as in the owner of the device)?


Thanks!

Accepted Reply

>But you can create your own 'creatorUserRecordID Name' and use that.

I wouldn't recommend creating another field since it's already available for use.


To fetch only a specific user records, you need to create a CKReference pointing to that user id (recordID), then use a predicate on that. It looks like this:

First fetch the user record id

yourContainer.fetchUserRecordIDWithCompletionHandler { (userID, error) -> Void in
     if let userID = userID {
          // here's your userID (recordID) to play with
     }
}

Then construct the predicate:

let reference = CKReference(recordID: userID, action: .None)
let predicate = NSPredicate(format: "creatorUserRecordID == %@", reference)
let query = CKQuery(recordType: "Species", predicate: predicate)

And then use the query as normal.


Happy fetching!

Replies

>How can I only fetch records that were created by the current user (as in the owner of the device)?


You may be able to use the metadata creatorUserRecordID in a predicate, I don't know how to do that.


But you can create your own 'creatorUserRecordID Name' and use that. Create a field ("TheName") in the CKRecord and when you first create the CKRecord load that field with a string that is specific to the user on their device (NSString *thisDevicesName=....) then change your predicate from:

      let query = CKQuery(recordType: "Species", predicate: NSPredicate(format: "TRUEPREDICATE"))

to

      NSPredicate *thePredicate=[NSPredicate predicateWithFormat:@"TheName = %@",thisDevicesName];
      let query = CKQuery(recordType: "Species", predicate: thePredicate)

>But you can create your own 'creatorUserRecordID Name' and use that.

I wouldn't recommend creating another field since it's already available for use.


To fetch only a specific user records, you need to create a CKReference pointing to that user id (recordID), then use a predicate on that. It looks like this:

First fetch the user record id

yourContainer.fetchUserRecordIDWithCompletionHandler { (userID, error) -> Void in
     if let userID = userID {
          // here's your userID (recordID) to play with
     }
}

Then construct the predicate:

let reference = CKReference(recordID: userID, action: .None)
let predicate = NSPredicate(format: "creatorUserRecordID == %@", reference)
let query = CKQuery(recordType: "Species", predicate: predicate)

And then use the query as normal.


Happy fetching!