iPhone CoreData migration fails with “Can't find model for source store”

I have an iPhone app that is using CoreData. I recently made some minor changes to the data model and now every time the app is opened I get an error "Can't find model for source store".

I have 2 version of the data model and the only changes I've made were some additions of some fields. I was following the guide here which worked initially, then just today, after adding some additional fields, it breaks. All additional fields are marked as optional and all have default values. The migration code is below:

NSURL *storeUrl = [NSURL fileURLWithPath:[[self applicationDocumentsDirectory] stringByAppendingPathComponent:@"xxx.sqlite"]];

// migration options
NSDictionary *options = [NSDictionary dictionaryWithObjectsAndKeys:
                         [NSNumber numberWithBool:YES], NSMigratePersistentStoresAutomaticallyOption,
                         [NSNumber numberWithBool:YES], NSInferMappingModelAutomaticallyOption, nil];

NSError *error = nil;
persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:[self managedObjectModel]];

if (![persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeUrl options:options error:&error]) {
...
}

The managedObjectModel is successfully created here:

- (NSManagedObjectModel *)managedObjectModel {

if (managedObjectModel != nil) {
    return managedObjectModel;
}

NSString *path = [[NSBundle mainBundle] pathForResource:@"DataModelName" ofType:@"momd"];
NSURL *momURL = [NSURL fileURLWithPath:path];
managedObjectModel = [[NSManagedObjectModel alloc] initWithContentsOfURL:momURL];

return managedObjectModel;
}

I've tracked down the issue to a mismatch in the version has for 1 entity. The error that gets thrown includes this has for the entity:

MyEntityName = ;

but the hash in the VersionInfo.plist in the app bundle is:

MyEntityName = ;

There are no other entities anywhere in the VersionInfo.plist with the hash .

8
задан Community 23 May 2017 в 12:04
поделиться