Core Data数据库条目丢失求助:Dime应用v2更新后新增交易消失
Core Data单例模式导致新增交易几天后消失(原架构无此问题)
我是基于Core Data开发的开源财务追踪应用Dime的开发者。在近期的v2版本更新中,我对Core Data架构做了调整——使用DataController的静态共享实例替代每次使用时的初始化操作,以此添加AppIntents功能,否则会出现“加载了两个数据模型”的错误。但更新后,我每天都会收到2-3封用户邮件反馈,称新增的交易在几天后退出应用时消失。该问题并非所有用户都会遇到,且我无法自行复现此问题。
新Core Data架构及使用方式
代码实现
class DataController: ObservableObject { static let shared = DataController() var container = NSPersistentCloudKitContainer(name: "MainModel") init() { let description = NSPersistentStoreDescription() description.shouldMigrateStoreAutomatically = true description.shouldInferMappingModelAutomatically = true description.setOption(true as NSNumber, forKey: NSPersistentHistoryTrackingKey) description.setOption(true as NSNumber, forKey: NSPersistentStoreRemoteChangeNotificationPostOptionKey) let keyValueStore = NSUbiquitousKeyValueStore.default if keyValueStore.object(forKey: "icloud_sync") == nil { keyValueStore.set(true, forKey: "icloud_sync") } if !keyValueStore.bool(forKey: "icloud_sync") { description.cloudKitContainerOptions = nil } else { description.cloudKitContainerOptions = NSPersistentCloudKitContainerOptions(containerIdentifier: "iCloud.com.rafaelsoh.dime") } let groupID = "group.com.rafaelsoh.dime" if let url = FileManager.default.containerURL(forSecurityApplicationGroupIdentifier: groupID) { description.url = url.appendingPathComponent("Main.sqlite") } container.persistentStoreDescriptions = [description] container.loadPersistentStores { description, error in if let error = error as NSError? { fatalError("Unresolved error \(error), \(error.userInfo) for \(description)") } self.container.viewContext.automaticallyMergesChangesFromParent = true } } }
使用方式
let dataController = DataController.shared
旧Core Data架构及使用方式(无交易丢失问题)
代码实现
class DataController: ObservableObject { var container = NSPersistentCloudKitContainer(name: "MainModel") init() { let description = NSPersistentStoreDescription() description.shouldMigrateStoreAutomatically = true description.shouldInferMappingModelAutomatically = true description.setOption(true as NSNumber, forKey: NSPersistentHistoryTrackingKey) description.setOption(true as NSNumber, forKey: NSPersistentStoreRemoteChangeNotificationPostOptionKey) if(!NSUbiquitousKeyValueStore.default.bool(forKey: "icloud_sync")){ description.cloudKitContainerOptions = nil } else { description.cloudKitContainerOptions = NSPersistentCloudKitContainerOptions(containerIdentifier: "iCloud.com.rafaelsoh.dime") } let groupID = "group.com.rafaelsoh.dime" if let url = FileManager.default.containerURL(forSecurityApplicationGroupIdentifier: groupID) { description.url = url.appendingPathComponent("Main.sqlite") } container.persistentStoreDescriptions = [description] container.loadPersistentStores { description, error in if let error = error as NSError? { fatalError("Unresolved error \(error), \(error.userInfo) for \(description)") } self.container.viewContext.automaticallyMergesChangesFromParent = true } } }
使用方式
struct dimeApp: App { @StateObject var dataController: DataController @UIApplicationDelegateAdaptor(AppDelegate.self) var appDelegate var body: some Scene { WindowGroup { ContentView() .environment(\.managedObjectContext, dataController.container.viewContext) .environmentObject(dataController) } } init() { let dataController = DataController() _dataController = StateObject(wrappedValue: dataController) } }
恳请各位提供技术帮助,非常感谢!
内容的提问来源于stack exchange,提问作者Rafael Soh
相关产品推荐
相关产品推荐

