如何查明 CloudKit 订阅何时被删除? [英] How to find out when a CloudKit Subscription is deleted?
问题描述
我在我的 iOS 应用程序中使用 CloudKit.
I am using CloudKit in my iOS application.
在我的应用程序中,每当用户修改某些数据时,我都会更新 CloudKit 私有数据库,以便用户的其他设备也可以更新.
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.
当用户启用同步时,我创建一个订阅并将本地数据推送到 CloudKit.
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.
应用程序显示一个 UISwitch
供用户启用/禁用同步.
The application shows a UISwitch
for the user to enable/disable syncing.
假设用户有 2 个设备 DeviceA
和 DeviceB
,这表明通过将 UISwitch.isOn
设置为 true 已启用同步.
Let us assume the user has 2 devices DeviceA
and DeviceB
which show that syncing has been enabled by setting UISwitch.isOn
to true.
如果用户在 DeviceA
上禁用同步,则订阅将被删除并且在 DeviceA
上所做的更改不会触发对 DeviceB
的远程通知预期.
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.
但是DeviceB
仍然显示同步已启用.
But DeviceB
still shows that syncing has been enabled.
有没有办法知道订阅何时被删除?
Is there a way to know when a subscription has been deleted?
我知道 CKFetchSubscriptionsOperation
.我可以定期调用 CKFetchSubscriptionsOperation
以了解订阅.有没有更好的方法?
I know about CKFetchSubscriptionsOperation
. I can call the CKFetchSubscriptionsOperation
periodically to know about the subscriptions. Is there a better way to this?.
推荐答案
这是一个很好的问题,也是我遇到的问题.你是对的.了解订阅状态的唯一方法是通过 CKFetchSubscriptionsOperation
查询可用的内容.
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
.
一种可能的解决方法是创建一个名为 Subscription
之类的 recordType
并将用户当前使用的 subscriptionID
保存为 常规 CloudKit 记录(只需在 CKRecord
上使用 String
属性).
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
).
然后,当他们在设备上取消订阅时,您可以更新 Subscription
记录,他们的所有设备都会收到有关更改的通知.然后,应用程序将根据用户可用的 subscriptionID
更新实际订阅.
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
取消订阅subscription1
.DeviceA
从Subscription
表中删除subscriptionRecord1
.DeviceA
使用CKModifySubscriptionsOperation()
删除实际的subscription1
订阅.DeviceB
收到通知subscriptionRecord1
已被删除,并将同步UISwitch
翻转到关闭状态(我假设您使用以下命令保存这些开关的状态)本地持久化方法,如数据库或UserDefaults
).
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.
这篇关于如何查明 CloudKit 订阅何时被删除?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!