Search code examples
iosxcode9core-data-migration

Coredata Lightweight Migration Fails


If I add a new model version and then add an attribute to an entity I get a migration failure. It doesn't matter what the type of the attribute is or whether it is optional or has a default value. If I just add the new model version but don't add the attribute it works OK. I have done this sort of thing lots of times over the past 3 years without problems. Both NSMigratePersistentStoresAutomaticallyOption and NSInferMappingModelAutomaticallyOption are set to true. The error I get is shown below.

2018-02-18 12:28:51.771608+0000 Writing Shed[4105:1886727] [error] error: Illegal attempt to save to a file that was never opened. "This NSPersistentStoreCoordinator has no persistent stores (schema mismatch or migration failure). It cannot perform a save operation.". Last recorded error = Error Domain=NSCocoaErrorDomain Code=134140 "Persistent store migration failed, missing mapping model."

Does anyone know what might be wrong?


Solution

  • I've found the problem. I'm developing an app on Mac OS that is already available on IOS. They sync CoreData via iCloud. At some point while working on the Mac I added some attributes to the model that weren't optional or defaulted. To cut a long story short this caused problems with an IOS store that predated the changes, so that's when the migration failed (can't migrate to non-optional/non-defaulted attributes). I'm still not sure how I got into this situation because I'm usually very careful with CoreData, but hey stuff happens. Thanks for the comments which made me look deeper.