Narwhal
Narwhal

Reputation: 784

Can't query based on User ID in Cloudkit

I don't understand why my ckquery does not fetch the records created by the user. Here is my code:

@IBAction func browseMyComplaints(sender: AnyObject) {
    // who am i?
    CKContainer.defaultContainer().fetchUserRecordIDWithCompletionHandler({ userRecordID, error in
        if error == nil {
            println(userRecordID.description)
            let query:CKQuery = CKQuery(recordType: "myComplaint", predicate: NSPredicate(format: "creatorUserRecordID = %@", userRecordID))

            var results : [CKRecord] = [CKRecord]()
            var operation : CKQueryOperation = CKQueryOperation(query: query)

            operation.recordFetchedBlock = { (record:CKRecord!) -> Void in
                results.append(record)
            }

            operation.queryCompletionBlock = {
                (cursor:CKQueryCursor!, error:NSError!) in
                if (error != nil) {
                    NSLog(error.description)
                } else {
                    NSLog("no query error reported")
                    NSLog(String(results.count) + " found")
                    tvComplaintRecords = results
                    self.performSegueWithIdentifier("complaintList", sender: self)
                }
            }
            publicDatabase.addOperation(operation)

        } else {
            println("Can not identify user due to error: \(error.localizedDescription)")
        }

    })

}

My output is:

<CKRecordID: 0x7be3f220; _46761404769b094e82053fea39b16bb5:(_defaultZone:__defaultOwner__)>
2014-10-11 12:45:32.597 ReportAMenace[15244:781048] no query error reported
2014-10-11 12:45:32.597 ReportAMenace[15244:781048] 0 found

But there are 11 records, include 1 that I made just before I ran the above query. Any hints?

Upvotes: 3

Views: 1891

Answers (2)

Narwhal
Narwhal

Reputation: 784

I figure it out. Of course there was an easy answer, although even then it's not entirely straight forward.

I had to go into the Cloudkit Dashboard (online website) and ensure the creator field, etc, had the query flag set to yes.

However, even then the old ones didn't show up (maybe they will later if Cloudkit does some kind of batch overnight indexing, not sure). But IT DID WORK for new records created from that moment on.

Upvotes: 2

Edwin Vermeer
Edwin Vermeer

Reputation: 13127

You could create a CKQuery for searching a recordID like this:

var query = CKQuery(recordType: recordType, predicate: NSPredicate(format: "%K == %@", "creatorUserRecordID" ,CKReference(recordID: userRecordID, action: CKReferenceAction.None)))

Upvotes: 2

Related Questions