You can get the model name and use it instead of model identifier. Check out this excellent article Custom Core Data Migrations and the corresponding Github code. With the help of the NSManagedObjectModel+MHWAdditions category you can retrieve the model name.
Source model name:
NSError *error;
NSString *storeType = ...;
NSURL *storeURL = ...;
NSDictionary *sourceMetadata = [NSPersistentStoreCoordinator metadataForPersistentStoreOfType:storeType
URL:storeURL
error:&error];
NSManagedObjectModel *sourceModel = [NSManagedObjectModel mergedModelFromBundles:@[[NSBundle mainBundle]]
forStoreMetadata:sourceMetadata];
NSString *sourceModelName = [sourceModel mhw_modelName];
Destination model name:
NSString *destinationModelName = [[self managedObjectModel] mhw_modelName];
Assuming you implemented a managedModelObject
getter. If not, here is out-of-the-box implementation.
- (NSManagedObjectModel *)managedObjectModel {
if (_managedObjectModel != nil) {
return _managedObjectModel;
}
NSString *momPath = [[NSBundle mainBundle] pathForResource:@"YourModel" ofType:@"momd"];
if (!momPath) {
momPath = [[NSBundle mainBundle] pathForResource:@"YourModel" ofType:@"mom"];
}
NSURL *url = [NSURL fileURLWithPath:momPath];
_managedObjectModel = [[NSManagedObjectModel alloc] initWithContentsOfURL:url];
return _managedObjectModel;
}
When migrating, the source model name and destination model name will differ. Otherwise, the names will be the same.