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, so people 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 work this will be, I’ve been kicking this can down the road for a while now. (See also: compliance to Swift 6’s strict concurrency). This week, I’ve started to really dig in.
Today I hit a wall, and I’m too tired and exasperated to do a long and involved write-up with pictures and stuff. The short-short version is:
Make sure you add indexes before adding data that you need to query that relies 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 a
new record type (“table”, sorta-kinda) called FavoriteList
, which is peer
to 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. Thanks
to some genuinely (and uncharacteristically for Apple) helpful sample code,
I was able to put something together quickly. However, it didn’t work. Thanks
to some genuinely (and uncharacteristically for Apple) helpful error messaging,
I quickly realized I needed to add an index to Favorite
; specifically for
this 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 different
reason: in trying to get favorites that are part of a given list, I was coming
up short. No matter what I tried, no records were being returned. When I looked
at the records in the iCloud Developer Dashboard, it appeared everything was
good. I could click from the field in Favorite
and it would load the list
in FavoriteList
. But whenever I tried to query using my code, it wouldn’t
work. I would come up with no records.
In chatting with some incredibly kind folks on Slack, one of them asked me to 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 to believe that my own code was not — for once — the issue.
After a couple hours of this, and in desperation, I deleted all my existing
Favorite
and added new ones. I tried my queries again, and they worked no
sweat. Both on the dashboard and in my code.

Thinking about this some more, and in chatting with the good samaritans on Slack, 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 no results at all) from a new field in CloudKit, consider whether the data you’ve added was done so before or after you added the corresponding index.
Apple folks, FB15563372.