user10461683
user10461683

Reputation:

How to find out when a CloudKit Subscription is deleted?

I am using CloudKit in my iOS application.

In my application, whenever user modifies some data, I update the CloudKit private db so that other devices of the user can also get updated.

This syncing mechanism can be enabled/disabled by the user.

When user enables syncing, I create a subscription and push the local data to CloudKit.

If the user has logged on to other devices, they start getting remote notifications about changes to the private database as expected.

The application shows a UISwitch for the user to enable/disable syncing.

Let us assume the user has 2 devices DeviceA and DeviceB which show that syncing has been enabled by setting UISwitch.isOn to true.

If the user disables syncing on DeviceA, then the subscription is deleted and changes made on DeviceA do not trigger remote notifications to DeviceB as expected.

But DeviceB still shows that syncing has been enabled.

Is there a way to know when a subscription has been deleted?

I know about CKFetchSubscriptionsOperation. I can call the CKFetchSubscriptionsOperation periodically to know about the subscriptions. Is there a better way to this?.

Upvotes: 3

Views: 158

Answers (1)

Clifton Labrum
Clifton Labrum

Reputation: 14128

This is a great question and is something I ran in to as well. You are correct. The only way to know the status of a subscription is to query for what's available with CKFetchSubscriptionsOperation.

One possible workaround is to create a recordType called something like Subscription and save the subscriptionIDs the user is currently using as regular CloudKit records (just use a String property on a CKRecord).

Then when they unsubscribe on a device, you can update the Subscription record and all their devices will get notified of the change. The app would then update the actual subscriptions based on the subscriptionIDs the user has available.

So here's a potential workflow:

  1. DeviceA unsubscribes from subscription1.
  2. DeviceA deletes subscriptionRecord1 from the Subscription table.
  3. DeviceA deletes the actual subscription1 subscription using CKModifySubscriptionsOperation().
  4. DeviceB gets notified that subscriptionRecord1 was deleted and flips the sync UISwitch to off (I presume you are saving the state of these switches with a local persistence method like a database or UserDefaults).

Hopefully that helps. Let me know if you have any questions.

Upvotes: 1

Related Questions