5

I have created new version of CoreData model and migrated existing one to it. Application works without any issues on iOS 9+, but for iOS 9 and 10 I am getting this error:

2017-10-22 19:28:37.081 CafeManager[16654:1918728] CoreData: Failed to load optimized model at path '/Users/dj-glock/Library/Developer/CoreSimulator/Devices/A81AA9C4-7B59-4422-BA0A-0FD0D1A05205/data/Containers/Bundle/Application/DD66571C-4EB6-4A8B-A99B-B447DD0FFFBA/CafeManager.app/CafeManager.momd/CafeManager v2.omo'

iOS 10:

CoreData: annotation: Failed to load optimized model at path '/Users/dj-glock/Library/Developer/CoreSimulator/Devices/3708F142-3BD0-4C70-8515-217B7785D285/data/Containers/Bundle/Application/3842402F-BEAE-47CB-8C27-EC6CA7D76B03/CafeManager.app/CafeManager.momd/CafeManager v2.omo'

I have checked a lot of similar questions, but did not find solution for me. I have tried to re-install app on simulator, to re-launch and so on. Can anybody advise?

Folder contains the following files:

enter image description here

AppDelegate CoreData stack for iOS 9:

// MARK: - Core Data stack for iOS 8+
static var managedObjectContext: NSManagedObjectContext = {
    var applicationDocumentsDirectory: URL = {
        let urls = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)
        return urls[urls.count-1]
    }()

    var managedObjectModel: NSManagedObjectModel = {
        let modelURL = Bundle.main.url(forResource: "CafeManager", withExtension: "momd")!
        return NSManagedObjectModel(contentsOf: modelURL)!
    }()

    var persistentStoreCoordinator: NSPersistentStoreCoordinator = {
        let a = managedObjectModel.entities
        let coordinator = NSPersistentStoreCoordinator(managedObjectModel: managedObjectModel)
        let url = applicationDocumentsDirectory.appendingPathComponent("SingleViewCoreData.sqlite")
        print(url)
        do
        {
            let options = [NSMigratePersistentStoresAutomaticallyOption: true, NSInferMappingModelAutomaticallyOption: true]
            try coordinator.addPersistentStore(ofType: NSSQLiteStoreType, configurationName: nil, at: url, options: options)
        } catch {
            NSLog("Error initializing smStore for iOS 8+ - \(error.localizedDescription)")
        }
        return coordinator
    }()
    let coordinator = persistentStoreCoordinator
    var managedObjectContext = NSManagedObjectContext(concurrencyType: .mainQueueConcurrencyType)
    managedObjectContext.persistentStoreCoordinator = coordinator
    return managedObjectContext
}()

Apple developer forum

Possible duplicate of this question.

DJ-Glock
  • 1,277
  • 2
  • 12
  • 39
  • Are you using any pods? – Alex Ioja-Yang Oct 24 '17 at 09:00
  • Yes.I use Seam3. Also I have imported Charts framework, but not via pods. – DJ-Glock Oct 24 '17 at 09:16
  • If it's possible try to run it without the frameworks and simply call CoreData before they are being used (to avoid NSException crash). Not very likely but there have been cases where frameworks were causing these issues so might be worth a check. – Alex Ioja-Yang Oct 24 '17 at 09:50
  • 2
    Not sure if you have solved it yet, but I fixed mine by rewriting the CoreDataModel. Took a screenshot of the content, deleted the file and created a new CoreDataModel. Then rewrote all the attributes and for some reason problem solved. Hope it helps – Alex Ioja-Yang Oct 30 '17 at 12:19
  • @AlexIoja-Yang hmm.. thanks for your input. I think I'll try this! It's sad that we face issues like this with XCode. – DJ-Glock Oct 30 '17 at 12:31
  • @AlexIoja-Yang unfortunately it did not help me. – DJ-Glock Oct 30 '17 at 15:59
  • 1
    @DJ-Glock I know this is fairly old question but if you still need solution please check out my [suggestion](https://stackoverflow.com/a/53973476/82813) – manikal Jan 08 '19 at 08:05
  • @mijokaliger I saw it. I need some time to check it, overloaded with other work. Appreciate your help, mate. – DJ-Glock Jan 09 '19 at 20:02
  • 1
    @mijokaliger thanks for your help! I have used your approach. It works properly. – DJ-Glock Jan 10 '19 at 19:57

1 Answers1

8

It seems to be the problem with optimized model versions on < iOS 11. Just use unoptimized .mom model version instead (CafeManager v2.mom).

Here's how I fixed it:

public func managedObjectModel() -> NSManagedObjectModel {
    let omoURL = modelBundle.url(forResource: name, withExtension: "omo", subdirectory: modelDirectoryName)
    let momURL = modelBundle.url(forResource: name, withExtension: "mom", subdirectory: modelDirectoryName)
    guard var url = omoURL ?? momURL else { fatalError("model version \(self) not found") }
    // Use unoptimized model version < iOS 11
    if #available(iOS 11, *) {} else { if let momURL = momURL { url = momURL} }
    guard let model = NSManagedObjectModel(contentsOf: url) else { fatalError("cannot open model at \(url)") }
    return model
}

If you're thinking, I want my speed, why would I use something unoptimized, read this answer.

manikal
  • 953
  • 7
  • 30