long story short, my Core Data schema was changed, and my app was submitted to the app store, which caused everybody who updated their app to crash. The crash is caused by a missing NSPersistentStore
due to not migrating the data model properly.
I still see the .sqlite and associated database files in the documents directory, and if I downgrade to the older version everything works fine with all data. My question is, can I recover from this with an app update by somehow migrating the existing NSPersistentStore
and adding it to the NSPersistentStoreCoordinator?
EDIT: so I didn't actually change my xcdatamodel myself, hence, "long story short". However, I did remove XMPPFramework from my project and I have a feeling this might have caused the core data problem.
EDIT:
I didn't make any changes directly to my data model, but I have pinpointed the problem. I was retrieving my NSManagedObjectModel
using [NSManagedObjectModel mergedModelFromBundles:nil]
which merges ALL data models present in the bundle. This included all data models that came with the XMPPFramework, and now that the framework has been removed, the NSManagedObjectModel that is passed into [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel]
is different, thus causing the crash.
I resolved the problem by using FMDB to fetch the contents of the existing DB, then created a new sqlite file and pointed the persistent store coordinator to that new sqlite file. Then I just inserted all the existing data into the new database by looping through the existing data and creating the appropriate NSManagedObject
s. I also stopped using mergedModelFromBundles
to retrieve my data model and instead use initWithContentsOfURL
.