如何查明 CloudKit 订阅何时被删除? [英] How to find out when a CloudKit Subscription is deleted?

查看:21
本文介绍了如何查明 CloudKit 订阅何时被删除?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的 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 个设备 DeviceADeviceB,这表明通过将 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 subscriptionIDs 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 subscriptionIDs the user has available.

这里有一个潜在的工作流程:

So here's a potential workflow:

  1. DeviceA 取消订阅 subscription1.
  2. DeviceASubscription 表中删除 subscriptionRecord1.
  3. DeviceA 使用 CKModifySubscriptionsOperation() 删除实际的 subscription1 订阅.
  4. DeviceB 收到通知 subscriptionRecord1 已被删除,并将同步 UISwitch 翻转到关闭状态(我假设您使用以下命令保存这些开关的状态)本地持久化方法,如数据库或 UserDefaults).
  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.

这篇关于如何查明 CloudKit 订阅何时被删除?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆