如何解决MacCatalyst 上的核心数据 CloudKit 同步问题
我遇到了最奇怪的问题。我开发了一款适用于 iPhone、iPad 和 MacCatalyst 的 SwiftUI 应用,使用 Core Data 和 CloudKit 私有数据库来同步用户所有设备上的数据。
问题是,当我在 iOS 设备(iPhone 或 iPad)上进行更新时,更新会在所有 iOS 设备上同步,但不会同步到 Mac。同样,我在 Mac 上进行的更新会在 Mac 之间同步,但不会同步到 iOS 设备。
如果我删除 Mac 上的应用程序及其关联的 ~/Library/Container/
现在,如果我将应用程序编译到 Mac 上(而不是安装存档应用程序),则来自 iOS 设备的同步会通过。但只有一次。在我再次将应用程序编译到 Mac 上之前,不会同步后续更新。同样,当我在 Mac 上编译应用程序时,在 Mac 上进行的更新只会同步(一次)到 iOS 设备。重新编译到 iOS 设备没有任何区别。
PersistentCloudkitContainer 类:
public class PersistentCloudKitContainer {
// MARK: - Define Constants / Variables
public static var context: NSManagedObjectContext {
return persistentContainer.viewContext
}
// MARK: - Initializer
private init() {}
// MARK: - Core Data stack
public static var persistentContainer: NSPersistentContainer = {
let container = NSPersistentCloudKitContainer(name: "MyModel")
container.loadPersistentStores(completionHandler: { (storeDescription,error) in
if let error = error as NSError? {
fatalError("Unresolved error \(error),\(error.userInfo)")
}
})
guard let description = container.persistentStoreDescriptions.first else {
fatalError("### PersistentCloudKitContainer->\(#function): Failed to retrieve persistant store description")
}
description.setOption(true as NSNumber,forKey: NSPersistentHistoryTrackingKey)
description.cloudKitContainerOptions?.databaseScope = .private
container.viewContext.automaticallyMergesChangesFromParent = true
container.viewContext.mergePolicy = NSMergeByPropertyObjectTrumpMergePolicy
return container
}()
// MARK: - Core Data Saving support
public static func saveContext () {
let context = persistentContainer.viewContext
if context.hasChanges {
do {
try context.save()
} catch {
let nserror = error as NSError
fatalError("Unresolved error \(nserror),\(nserror.userInfo)")
}
}
}
持久容器由 SwiftUI App 结构调用:
struct MyApp: App {
let context = PersistentCloudKitContainer.persistentContainer.viewContext
var body: some Scene {
WindowGroup {
ContentView().environment(\.managedObjectContext,context)
}
}
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。