问题
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?.
回答1:
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 subscriptionID
s 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 subscriptionID
s the user has available.
So here's a potential workflow:
DeviceA
unsubscribes fromsubscription1
.DeviceA
deletessubscriptionRecord1
from theSubscription
table.DeviceA
deletes the actualsubscription1
subscription usingCKModifySubscriptionsOperation()
.DeviceB
gets notified thatsubscriptionRecord1
was deleted and flips the syncUISwitch
to off (I presume you are saving the state of these switches with a local persistence method like a database orUserDefaults
).
Hopefully that helps. Let me know if you have any questions.
来源:https://stackoverflow.com/questions/55741178/how-to-find-out-when-a-cloudkit-subscription-is-deleted