【发布时间】:2022-04-25 03:45:11
【问题描述】:
我正在构建一个 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同步?
非常感谢任何帮助。
【问题讨论】:
标签: swift macos core-data cloudkit