This NSPersistentStoreCoordinator has no persistent stores (schema mismatch or migration failure). It cannot perform a save operation
You don't have to do the migration yourself here. You do have to add a new version of the data model. You can't edit the xcdatamodel
and expect Core Data to just use the new version. You need to keep your existing model, create a new version, and make your changes in the new version. You must always have a version of the model that matches the persistent store file.
You create a new version by selecting the xcdatamodel
model file in Xcode's file browser, going to the "Editor" menu, and selecting "Add Model Version..."
I work on a project and faced a similar problem, it seems that the former developer forgot to pass these two options for a lightweight migration. I passed in the second one and the migration completed successfully.
You request automatic lightweight migration using the options dictionary you pass in
addPersistentStoreWithType:configuration:URL:options:error:
, by setting values corresponding to both theNSMigratePersistentStoresAutomaticallyOption
and theNSInferMappingModelAutomaticallyOption
keys toYES
:
NSError *error = nil;
NSURL *storeURL = [[NSBundle mainBundle] URLForResource:@"Model" withExtension:@"momd"];
NSPersistentStoreCoordinator *psc = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:managedObjectModel]
NSDictionary *options = [NSDictionary dictionaryWithObjectsAndKeys: [NSNumber numberWithBool:YES], NSMigratePersistentStoresAutomaticallyOption, [NSNumber numberWithBool:YES], NSInferMappingModelAutomaticallyOption, nil];
BOOL success = [psc addPersistentStoreWithType:NSSQLiteStoreType
configuration:nil URL:storeURL
options:options error:&error];
if (!success) {
// Handle the error.
}