Callsheet — my app that’s like IMDB but for people with taste —uses iCloud for several things, notably, storing your “pinned items”. Internally,I call pinned items “favorites”, so in this post I’ll refer to them interchangably.Regardless, today, there’s only one list of pinned items.
My most-requested feature is to add support for multiple lists. Generally, sopeople can have things like To Watch, Watching, and Did Watch. Naturally,your particular needs may differ, but this request is common… and constant.
Due mostly to life obligations (all is well), but also my fear of how much workthis will be, I’ve been kicking this can down the road for a while now. (Seealso: compliance to Swift 6’s strict concurrency). This week, I’vestarted to really dig in.
Today I hit a wall, and I’m too tired and exasperated to do a long and involvedwrite-up with pictures and stuff. The short-short version is:
Make sure you add indexes before adding data that you need to query thatrelies on those indexes; if you don’t, they may not work as expected.
When tackling multiple lists of pins, the first thing I did was to add anew record type (“table”, sorta-kinda) called FavoriteList
, which is peerto the pre-existing Favorite
. Then I added a new field (“column”,sorta-kinda) to Favorite
that contains a CKRecord.Reference
to the favorite/pin’s parent list.
Once I got the schema updated in development, I started writing code. Thanksto some genuinely (and uncharacteristically for Apple) helpful sample code,I was able to put something together quickly. However, it didn’t work. Thanksto some genuinely (and uncharacteristically for Apple) helpful error messaging,I quickly realized I needed to add an index to Favorite
; specifically forthis new reference field I added.
I added the compulsory indexes, and then went back to writing code.
Quickly, I was flummoxed; things still weren’t working. Now, for a differentreason: in trying to get favorites that are part of a given list, I was comingup short. No matter what I tried, no records were being returned. When I lookedat the records in the iCloud Developer Dashboard, it appeared everything wasgood. I could click from the field in Favorite
and it would load the listin FavoriteList
. But whenever I tried to query using my code, it wouldn’twork. I would come up with no records.
In chatting with some incredibly kind folks on Slack, one of them asked meto try to replicate the query I was doing in code on the Developer Dashboard.I got the same empty results. This was a critical tip, because it led me tobelieve that my own code was not — for once — the issue.
After a couple hours of this, and in desperation, I deleted all my existingFavorite
and added new ones. I tried my queries again, and they worked nosweat. Both on the dashboard and in my code.

Thinking about this some more, and in chatting with the good samaritans onSlack, we all came to the same conclusion: the index probably didn’t properly…uh… index… whatever data was already there before the index was created.
So, if you’re ever in a situation where you’re getting wonky results (or noresults at all) from a new field in CloudKit, consider whether the data you’veadded was done so before or after you added the corresponding index.
Apple folks, FB15563372.