Ios 让Swift 4.2从先前的Objective-C构建加载核心数据存储

Ios 让Swift 4.2从先前的Objective-C构建加载核心数据存储,ios,swift,core-data-migration,Ios,Swift,Core Data Migration,所以我有一个旧的ObjC核心数据应用程序,我正在Swift 4.2中从头重写,但我无法让Swift的PersistentContainer从ObjC找到NSManagedObjectModel。应用程序已经在AppStore中,因此我显然需要我的新Swift版本来加载用户设备上已经存在的当前存储 当我打印出原始ObjC模型的目录时,它会显示: /Users/xxxx/Library/Developer/CoreSimulator/Devices/2DB3B49C-E397-427E-A30C-2

所以我有一个旧的ObjC核心数据应用程序,我正在Swift 4.2中从头重写,但我无法让Swift的PersistentContainer从ObjC找到NSManagedObjectModel。应用程序已经在AppStore中,因此我显然需要我的新Swift版本来加载用户设备上已经存在的当前存储

当我打印出原始ObjC模型的目录时,它会显示:

/Users/xxxx/Library/Developer/CoreSimulator/Devices/2DB3B49C-E397-427E-A30C-273C2738B253/data/Containers/Data/Application/7B1BCD4F-91F8-45AD-9634-4F1D71205EEB/Documents/Model.sqlite
/Users/xxxx/Library/Developer/CoreSimulator/Devices/2DB3B49C-E397-427E-A30C-273C2738B253/data/Containers/Data/Application/AB13D4B1-01E2-477D-B6D1-295F4AD0F1BB/Documents/Model.sqlite
。。。但当我在我的快速重写中做同样的事情时,它会告诉我:

/Users/xxxx/Library/Developer/CoreSimulator/Devices/2DB3B49C-E397-427E-A30C-273C2738B253/data/Containers/Data/Application/7B1BCD4F-91F8-45AD-9634-4F1D71205EEB/Documents/Model.sqlite
/Users/xxxx/Library/Developer/CoreSimulator/Devices/2DB3B49C-E397-427E-A30C-273C2738B253/data/Containers/Data/Application/AB13D4B1-01E2-477D-B6D1-295F4AD0F1BB/Documents/Model.sqlite
“Documents”目录前的最后一个数字字符串非常不同

由于存储不在同一个目录中,我的Swift rewrite只是创建了一个全新的存储并从那里开始。有趣的是,如果我在模拟器中加载我的旧ObjC版本“返回”Swift重写,它仍然会加载原始存储-因此在两个不同的目录中显然有两个完全不同的存储

如何让我当前的Swift重写加载旧商店?Swift 4.2现在正在使用PersistentContainer,我想我可以通过以下方式获取原始存储:

let dirPaths = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)
。。。但它仍然不起作用。我仍然无法找到并迁移旧商店。我不敢相信我是唯一一个经历过这种情况的人,但我一生都找不到任何有效的建议。有人吗

我试过了

lazy var persistentContainer: NSPersistentContainer = {
        let container = NSPersistentContainer(name: "Model")

        let storeDirectory = FileManager.default.urls(for: .applicationSupportDirectory, in: .userDomainMask).first!
        let url = storeDirectory.appendingPathComponent("Model.sqlite")
        let description = NSPersistentStoreDescription(url: url)
        description.shouldInferMappingModelAutomatically = true
        description.shouldMigrateStoreAutomatically = true
        container.persistentStoreDescriptions = [description]

        container.loadPersistentStores(completionHandler: { (storeDescription, error) in
            print("loading stores")
            if let error = error as NSError? {
                fatalError("Unresolved error \(error), \(error.userInfo)")
            }
        })

        return container
    }()

但是它仍然没有加载旧的商店模式。

对于那些有类似更新问题的人来说,大量的尝试和错误似乎已经提供了答案。以下是我如何在Swifts new persistentContainer中找到旧商店的:

let paths = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)
let filePath = paths[0].appendingPathComponent("Model.sqlite")
let description = NSPersistentStoreDescription(url: filePath)
description.shouldMigrateStoreAutomatically = true
description.shouldInferMappingModelAutomatically = true
container.persistentStoreDescriptions = [description]

感谢所有的人。

对于那些有类似更新问题的人,大量的尝试和错误似乎已经提供了答案。以下是我如何在Swifts new persistentContainer中找到旧商店的:

let paths = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)
let filePath = paths[0].appendingPathComponent("Model.sqlite")
let description = NSPersistentStoreDescription(url: filePath)
description.shouldMigrateStoreAutomatically = true
description.shouldInferMappingModelAutomatically = true
container.persistentStoreDescriptions = [description]

谢谢大家。

上面的代码执行了吗?您是否已完成?哪里出错了?看起来你的应用程序是另一个应用程序。您是否更改了捆绑标识?Mike-捆绑标识是相同的。Swift版本将覆盖模拟器中的ObjC版本,但加载不同的存储。然后,我可以在模拟器中的Swift版本上重新加载ObjC版本,它显示了旧的存储区-因此在那里有2个存储区-但是该应用程序在模拟器中只存在一次。Koen-在lazy var persistentContainer中没有任何错误。一直到container.loadPersistentStores,输出“loading stores”并返回容器。只是装错了容器。这是一个新的,而不是先前存在的。如果你运行ObjC或Swift应用程序,它是否总是链接到相同的路径?上面的代码是否已执行,你是否已通过它?哪里出错了?看起来你的应用程序是另一个应用程序。您是否更改了捆绑标识?Mike-捆绑标识是相同的。Swift版本将覆盖模拟器中的ObjC版本,但加载不同的存储。然后,我可以在模拟器中的Swift版本上重新加载ObjC版本,它显示了旧的存储区-因此在那里有2个存储区-但是该应用程序在模拟器中只存在一次。Koen-在lazy var persistentContainer中没有任何错误。一直到container.loadPersistentStores,输出“loading stores”并返回容器。只是装错了容器。这是一个新的,而不是先前存在的。如果您运行ObjC或Swift应用程序,它是否总是链接到同一路径?