Search code examples
swiftcloudkit

CKFetchRecordsOperation never returns any keys


I have a single record in my public (development) database, with several populated fields. When I use a CKFetchRecordsOperation to fetch that record, the fetch succeeds, but the resulting CKRecord always contains no keys. This happens whether I explicitly set desiredKeys on my fetch operation, or leave it as nil. The record and its keys have been unchanged for a long time, so it's not a propagation delay.

There's only one record in the public database and its ID matches what I get, so I'm sure the correct record is being fetched; it just never has any keys. For example, the below code always prints keys: [].

let op = CKFetchRecordsOperation(recordIDs:[...])
op.desiredKeys = [...] // commenting this has no effect
op.fetchRecordsCompletionBlock = {records, error in
    if let e = error {
        print("error:", e)}
    for (id, r) in records ?? [:] {
        print("keys:", r.allKeys())}}
db.addOperation(op)

I've also tried accessing the individual keys via subscript notation, but they're always nil. I'm at a loss at this point. There are no errors, and the record I want is being found, so why are there never any keys?

Update: I've noticed that the CloudKit Dashboard always says "Reindexing development" in the bottom left corner, even days after my last change. Furthermore, I tried creating a second record (via the Dashboard) and my fetch fails to even find it, after leaving plenty of time for propagation.

Following a link from this SO question to an Apple developer forum thread about this problem, I found one post suggesting it happens only to records with Asset fields. I will try experimenting when I have time, though I can't avoid using Assets in this case so it might not be much help.

Update 2: I tried resetting my development environment and then recreating the same records as before. On the plus side, the "Reindexing" message in the CloudKit Dashboard is finally gone. Unfortunately, my code now fails to find any of my records, even though the code remains unchanged and I've triple-checked everything.

I also tried creating a record type with no Asset fields (see first update above), but it didn't seem to change anything.


Solution

  • Turns out I was querying the private database, when the records I wanted were in the public one.