3

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?

Narwhal
  • 744
  • 1
  • 8
  • 22

2 Answers2

2

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)))
Edwin Vermeer
  • 13,017
  • 2
  • 34
  • 58
  • I know you are right, but it just doesn't work. I don't understand it. My other queries work fine. println(userRecordID.description) var query = CKQuery(recordType: "myComplaint", predicate: NSPredicate(format: "%K == %@", "creatorUserRecordID" ,CKReference(recordID: userRecordID, action: CKReferenceAction.None))) – Narwhal Oct 12 '14 at 21:11
2

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.

Narwhal
  • 744
  • 1
  • 8
  • 22
  • Ah, I had a similar issue about that before. I wish apple would made all meta data fields searchable and sortable by default. Or add functionality to CloudKit to make them so by code. – Edwin Vermeer Oct 13 '14 at 05:42
  • I agree. The old records never did show up. This would be really bad if you already had a large data set and needed to turn on the flag for additional development. – Narwhal Oct 14 '14 at 18:32