core-data - NSPersistentStoreRemoteChangeNotification 没有被解雇
问题描述
我正在尝试在我的 CoreData+CloudKit 项目中执行历史跟踪,该项目使用NSPersistentCloudKitContainer
. 我一直在关注 Apple 的示例项目
我想在远程存储更新后执行某些任务。为此,苹果建议在应用程序的“签名和功能的后台模式”部分启用远程通知。
我已经为我的项目启用了历史跟踪,如 Apple 的示例项目所示。
// turn on persistent history tracking
let description = container.persistentStoreDescriptions.first
description?.setOption(true as NSNumber,
forKey: NSPersistentHistoryTrackingKey)
// ...
我也注册了我的商店来监听商店的变化。
// turn on remote change notifications
let remoteChangeKey = "NSPersistentStoreRemoteChangeNotificationOptionKey"
description?.setOption(true as NSNumber,
forKey: remoteChangeKey)
// ...
还添加了观察者来监听NSPersistentStoreRemoteChangeNotification
。
然而,没有NSPersistentStoreRemoteChangeNotification
被解雇。为了确保我的实现没有错误,我只是在@objc func storeRemoteChange(_ notification: Notification)
Apple 提供的示例代码中放置了断点,但我仍然看不到任何通知被触发并且没有断点被激活。
我已经了解在示例项目中完成的标签的重复数据删除,并尝试对其进行测试,但没有任何成功。这是 Apple 实施中的错误还是我缺少任何所需的设置?
解决方案
我的猜测是你正在观察容器而不是商店协调员,像这样添加你的观察者:
NotificationCenter.default.addObserver(
self, selector: #selector(type(of: self).storeRemoteChange(_:)),
name: .NSPersistentStoreRemoteChange, object: container.persistentStoreCoordinator)
注意最后一个参数container.persistentStoreCoordinator
还有一个警告,这个通知会出现在所有不同的线程上,所以你要小心并发。只需在该方法中放置 5 秒睡眠,您就会在应用启动时看到 3 个不同的线程调用它。这可能就是为什么在示例中有一个historyQueue
with maxOperationCount
1 来处理它的原因。
有些通知有NSPersistentHistoryTokenKey
在userInfo
不确定为什么。
推荐阅读
- javascript - 如何在 JavaScript 中使用 ScrollTo()
- html - 使标签与下面的语义 UI 宽度相同
- spring-boot - 将身份验证/授权移动到单独的微服务,然后结合网关(zuul + eureka 服务器)
- java - Vert.x 中的 sharedData AsyncMap 的寿命有多长?
- c# - log4net - 使用 .config 文件部分 (WebService/IIS) 配置存储库 [MyProject] 时出错
- android - 如何在 Flutter 中使用 Circle Items 制作 ListView 或 PageView 并调整当前 Circle 的大小?
- prolog - 在prolog中制作自然数规则的正确方法是什么?
- java - 即使使用设置,Eclipse 内容辅助自动完成也无法正常工作
- python - 字符串包含换行符 (\n),如何使用正则表达式将 \n 替换为 \n?
- c++ - 对全局变量的非常量引用的无效初始化