如何确定何时删除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
s保存为常规CloudKit记录(只需使用String
属性在CKRecord
上.
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屋!