【问题标题】:Going from MagicalRecord to NSPersistentContainer. How to preserve data?从 MagicalRecord 到 NSPersistentContainer。如何保存数据?
【发布时间】:2017-12-02 20:36:50
【问题描述】:

在当前运行的 App Store 应用程序中,我使用 MagicalRecord 来设置我的核心数据。当我面临一些并发问题时,我四处寻找解决方案。我意识到自从 iOS 10 推出以来,Core Data 已经被简化了,我可以直接使用它而无需 Magical Record。

神奇的记录设置

使用最新的存储库和 carthage,我使用以下行来设置核心数据。

    MagicalRecord.setupCoreDataStack(withAutoMigratingSqliteStoreNamed: "Model")

NSPersistentContainer 设置

lazy var persistentContainer: NSPersistentContainer = {
    let container = NSPersistentContainer(name: "Model")
    container.loadPersistentStores(completionHandler: { (storeDescription, error) in
        if let error = error as NSError? {
            fatalError("Unresolved error \(error), \(error.userInfo)")
        }
    })
    return container
}()

问题

这就像设置一个新数据库,我无法访问使用 Magical Record 设置存储的数据。

我试图查看 MagicalRecord 是否对所使用的名称进行了任何更改,但看起来并没有。有谁知道如何进行转换并且仍然能够访问我的旧数据。

【问题讨论】:

    标签: ios core-data magicalrecord


    【解决方案1】:

    您可能只需要更正商店 URL,因为它位于非标准位置。

    lazy var persistentContainer: NSPersistentContainer = {
        let container = NSPersistentContainer(name: "Model")
    
        // Change from the default store location
        let url = .... where is your database?
        let description = NSPersistentStoreDescription(url: url)
        container.persistentStoreDescriptions = [description]
    
        container.loadPersistentStores(completionHandler: { (storeDescription, error) in
            if let error = error as NSError? {
                fatalError("Unresolved error \(error), \(error.userInfo)")
            }
        })
        return container
    }()
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-08-01
      • 2017-08-16
      • 2014-07-09
      • 2018-02-26
      • 2017-05-20
      • 2012-02-03
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多