简体   繁体   中英

Getting Swift 4.2 to load Core Data Store from previous Objective-C build

So I've got an older ObjC Core Data App that I'm rewriting from the ground up in Swift 4.2 but I can't get the PersistentContainer of Swift to locate the NSManagedObjectModel from ObjC. The App is already in the AppStore so I obviously need my new Swift version to load the current store that's already on the user's device.

When I print out the directory of the original ObjC Model it shows me:

/Users/xxxx/Library/Developer/CoreSimulator/Devices/2DB3B49C-E397-427E-A30C-273C2738B253/data/Containers/Data/Application/7B1BCD4F-91F8-45AD-9634-4F1D71205EEB/Documents/Model.sqlite

... but when I do the same in my Swift rewrite it shows me:

/Users/xxxx/Library/Developer/CoreSimulator/Devices/2DB3B49C-E397-427E-A30C-273C2738B253/data/Containers/Data/Application/AB13D4B1-01E2-477D-B6D1-295F4AD0F1BB/Documents/Model.sqlite

The last string of numbers right before the 'Documents' directory are very different.

Since the Store isn't in the same directory my Swift rewrite just creates an entirely new store and goes from there. Interestingly enough, if I load my old ObjC version "back over" the Swift rewrite in the simulator it still loads the original store back up - so there's clearly 2 completely different stores in two different directories.

How do I get my current Swift rewrite to load up the old store? Swift 4.2 is now using PersistentContainer and I thought I'd be able to grab the original store using the following:

let dirPaths = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)

... but it still doesn't work. I'm still unable to locate and migrate the old store. I can't believe I'm the only person that's experienced this but for the life of me can't find any stackoverflow suggestions that work. Thoughts anyone?

I've tried

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
    }()

But it's still not loading the old store Model.

For those with similar update issues, a lot of trial and error seems to have provided the answer. Here's how I located the old store within 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]

Thanks all those out there.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM