如何解决CoreData 直到应用程序停用才更新
我正在构建一个 Macos CoreData+CloudKit 应用程序。
我首先通过这样做来创建我的容器:
let container = NSPersistentCloudKitContainer(name: "TestApp")
let description = container.persistentStoreDescriptions.first
description?.setOption(true as NSNumber,forKey: NSPersistentHistoryTrackingKey)
let remoteChangeKey = "NSPersistentStoreRemoteChangeNotificationOptionKey"
description?.setOption(true as NSNumber,forKey: remoteChangeKey)
container.loadPersistentStores(completionHandler: { (storeDescription,error) in
if let error = error {
fatalError("Unresolved error \(error)")
}
})
我正在注册以通过这样做来接收远程更改:
NotificationCenter.default.addObserver(
self,selector: #selector(fetchChanges),name: NSNotification.Name(
rawValue: "NSPersistentStoreRemoteChangeNotification"),object: persistentContainer.persistentStoreCoordinator
)
在 fetchChanges
中,我只需重新加载我的用户界面
- 获取数据
- 重新加载我的 tableView
但是,我发现 fetchChanges
在应用程序停用之前永远不会被调用。我在日志中看到这一点:
CoreData: debug: CoreData+CloudKit: -[PFCloudKitThrottledNotificationObserver noteRecievedNotification:](40): <PFCloudKitThrottledNotificationObserver: 0x600003fb3fc0>: Got: NSApplicationWillBecomeActiveNotification - 0
我假设正在发生的是,当 CoreData 通过 NSApplicationWillBecomeActiveNotification
事件检测到我的窗口停用时,它会生成一个后台任务以将远程 iCloud
数据提取到本地存储中。
此时,接收到 NSPersistantStoreRemoteChangeNotification
。
我的问题是如何强制本地商店与 iCloud
同步?
非常感谢任何帮助。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。