user3075898
user3075898

Reputation: 175

Managing CKQuerySubscriptions

I'd like to introduce the usage of CloudKit in an app I am developing. As a first step I'd use it for handling push notifications when certain records are inserted in the public database. So in my app I would create a CKQuerySubscription for each record type I'd like to monitor.

My question is: what's the best strategy to renew these subscriptions? While testing I noticed my development container messed up a few times and I had to reset it to get subscriptions to behave as they should. Apple does not state anything in the documentation, so when is one supposed to delete an existing subscription and recreate it? Since over time the notification info for a subscription might change, I thought of recreating all subscriptions each time the build number is different. Is this a good approach to avoid messing up the production version of the container?

Upvotes: 3

Views: 525

Answers (2)

Thunk
Thunk

Reputation: 4177

In my scenario, I added a check at startup to fetch all the subscriptions and validate that the expected subs are there. This way I can reset, change containers or even deal with vanishing subs (i have not seen subs vanish, but if it should happen, I can recover gracefully).

This would also give you a place to update subs, if your notifications need to change. Fetch them all then purge/replace as needed.

With that approach you don’t need to continually rebuild your subs. You’ll refresh just when they’re missing or you update your check logic to look for different subs.

Upvotes: 1

Adolfo
Adolfo

Reputation: 1862

The recommended strategy is first, create custom Record Zones and second, subscribe to database changes.

The best resource to learn about this is the WWDC 2016 CloudKit Best Pratices session.

Upvotes: 0

Related Questions