Ara*_*ash 3 synchronization core-data cloudkit
我正在尝试复制有关自动将核心数据与云工具包同步的WWDC讨论的结果。
我尝试了三种方法:
制作一个新的主从视图应用程序并按照wwdc 2019演讲中的步骤进行操作,在这种情况下不会发生同步
在这种情况下,也将下载示例wwdc 2019应用程序
我制作了一个带有少量核心数据和云工具包容器的小型应用程序,在这种情况下,发生了同步,但是我必须重新启动该应用程序。我怀疑这与历史记录管理有关,因此观察到NSPersistentStoreRemoteChange通知并非一无所获。
感谢任何帮助。
小智 7
我发现 NSPersistentStoreRemoteChange 通知是由 NSPersistentStoreCoordinator 发布的,而不是由 NSPersistentCloudKitContainer 发布的,所以下面的代码解决了这个问题:
// Observe Core Data remote change notifications.
NotificationCenter.default.addObserver(
self, selector: #selector(self.storeRemoteChange(_:)),
name: .NSPersistentStoreRemoteChange, object: container.persistentStoreCoordinator)
Run Code Online (Sandbox Code Playgroud)
I also played around with CoreData and iCloud and it work perfectly. I would like to list some important points that may help you go further:
Debug > Trigger iCloud SyncPush Notification and iCloud capability to your app. Make sure that you don't Dave issue with iCloud container (in this case, you will see red text on iCloud session in Xcode)container.viewContext.automaticallyMergesChangesFromParent = true. Code:
public lazy var persistentContainer: NSPersistentCloudKitContainer = {
/*
The persistent container for the application. This implementation
creates and returns a container, having loaded the store for the
application to it. This property is optional since there are legitimate
error conditions that could cause the creation of the store to fail.
*/
let container = NSPersistentCloudKitContainer(name: self.modelName)
container.viewContext.automaticallyMergesChangesFromParent = true
container.loadPersistentStores(completionHandler: { (storeDescription, error) in
if let error = error as NSError? {
// Replace this implementation with code to handle the error appropriately.
// fatalError() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development.
/*
Typical reasons for an error here include:
* The parent directory does not exist, cannot be created, or disallows writing.
* The persistent store is not accessible, due to permissions or data protection when the device is locked.
* The device is out of space.
* The store could not be migrated to the current model version.
Check the error message to determine what the actual problem was.
*/
fatalError("Unresolved error \(error), \(error.userInfo)")
}
})
return container
}()
Run Code Online (Sandbox Code Playgroud)
CloudKit: CoreData+CloudKit: ..........