i've read all possible blogs and SO post on the subject - but still not sure what's going on. I've also read this but still no luck - their guide to Default migration kind of clear but doesn't work in my situation . I'm relatively new to iOS development, so be gentle :)
Here is the situation: on the entity called Report in my app (iOS), following changes need to happen:
data attribute - deleted
title attribute - added
reportId attribute need to be changed from Integer 16 to String. This is what causing my problems. I did created new version of the my data model from my current one and modifying attributes.
First here is some methods from the app:
- (NSManagedObjectModel *)managedObjectModel
{
if (__managedObjectModel != nil)
{
return __managedObjectModel;
}
NSURL *modelURL = [[NSBundle mainBundle] URLForResource:@"App" withExtension:@"momd"];
__managedObjectModel = [[NSManagedObjectModel alloc] initWithContentsOfURL:modelURL];
return __managedObjectModel;
}
and then
/**
Returns the persistent store coordinator for the application.
If the coordinator doesn't already exist, it is created and the application's store added to it.
*/
- (NSPersistentStoreCoordinator *)persistentStoreCoordinator
{
if (__persistentStoreCoordinator != nil)
{
return __persistentStoreCoordinator;
}
NSURL *storeURL = [[self applicationDocumentsDirectory] URLByAppendingPathComponent:@"App.sqlite"];
NSError *error = nil;
__persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:[self managedObjectModel]];
NSLog(@"Which Current Version is our .xcdatamodeld file set to? %@", [[self managedObjectModel] versionIdentifiers]);
NSDictionary *options = [NSDictionary dictionaryWithObjectsAndKeys:
[NSNumber numberWithBool:YES], NSMigratePersistentStoresAutomaticallyOption,
//Commented for manual migration [NSNumber numberWithBool:YES], NSInferMappingModelAutomaticallyOption,
nil];
if (![__persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeURL options:options error:&error])
{
NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
abort();
}
return __persistentStoreCoordinator
}
First i've tried lightweight migration. It failed with
reason=Can't find mapping model for migration
Then i did the following just to try: made reportId attribute back to what it was (interger 16, keeping other two changes in the model. Lightweight migration worked just fine.
Ok, i thought, it probably need to have manual mapping in order to handle data type change. So, i've created mapping model ( in which i've tried to set reportId to both "", and source.reportId ), turned off lightweight migration. Since i'm new to this, i've decided to take baby steps and don't make this reportId Int > String change just yet and test my mapping. And it didn't work. With same reason=Can't find mapping model for migration. I've tried to change reportId to String as it should be - same result. It almost looks to me that my mapping model is completely ignored. In fact i've tried to delete it - same result same error. What the hell i'm missing here ?
## EDIT ##
Ok, i need to get to the bottom of this, I've downloaded the app that Mihai had put together ( thanks ! ) and started to play around with it. I've modified persistent store coordinator to match whatever i have for "Default Migration" to be this
- (NSPersistentStoreCoordinator *)persistentStoreCoordinator
{
if (__persistentStoreCoordinator != nil)
{
return __persistentStoreCoordinator;
}
NSURL *storeURL = [[self applicationDocumentsDirectory] URLByAppendingPathComponent:@"TestData.sqlite"];
NSError *error = nil;
__persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:[self managedObjectModel]];
NSDictionary *options = [NSDictionary dictionaryWithObjectsAndKeys:
[NSNumber numberWithBool:YES], NSMigratePersistentStoresAutomaticallyOption,
nil];
if (![__persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeURL options:options error:&error])
{
NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
abort();
}
return __persistentStoreCoordinator;
}
And i've got the error that totally make sense to me. It basically found right mapping model and try to match it and gave very valid error. 2012-02-07 10:47:39.246 TestData[2008:707] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Unacceptable type of value for attribute: property = "reportId"; desired type = NSString; given type = __NSCFNumber; value = 1.'
Where in my case i have dump that looks like this ( just a small part of the log ) :
Report = "(<NSEntityDescription: 0x1708d0>) name Report, managedObjectClassName Report, renamingIdentifier Report, isAbstract 0, superentity name (null), properties {\n action = \"(<NSAttributeDescription: 0x170a30>), name action, isOptional 1, isTransient 0, entity Report, renamingIdentifier action, validation predicates (\\n), warnings (\\n), versionHashModifier (null)\\n userInfo {\\n}, attributeType 100 , attributeValueClassName NSNumber, defaultValue 0\";\n data = \"(<NSAttributeDescription: 0x1709e0>), name data, isOptional 1, isTransient 0, entity Report, renamingIdentifier data, validation predicates (\\n), warnings (\\n), versionHashModifier (null)\\n userInfo {\\n}, attributeType 700 , attributeValueClassName NSString, defaultValue (null)\";\n reportId = \"(<NSAttributeDescription: 0x170a80>), name reportId, isOptional 1, isTransient 0, entity Report, renamingIdentifier reportId, validation predicates (\\n), warnings (\\n), versionHashModifier (null)\\n userInfo {\\n}, attributeType 100 , attributeValueClassName NSNumber, defaultValue 0\";\n timestamp = \"(<NSAttributeDescription: 0x170ad0>), name timestamp, isOptional 1, isTransient 0, entity Report, renamingIdentifier timestamp, validation predicates (\\n), warnings (\\n), versionHashModifier (null)\\n userInfo {\\n}, attributeType 900 , attributeValueClassName NSDate, defaultValue (null)\";\n type = \"(<NSAttributeDescription: 0x170990>), name type, isOptional 1, isTransient 0, entity Report, renamingIdentifier type, validation predicates (\\n), warnings (\\n), versionHashModifier (null)\\n userInfo {\\n}, attributeType 100 , attributeValueClassName NSNumber, defaultValue 0\";\n}, subentities {\n}, userInfo {\n}, versionHashModifier (null)";
}, fetch request templates {
}, reason=Can't find mapping model for migration}, {
Got to the point where i have 'NSInvalidArgumentException', reason: 'Mismatch between mapping and source/destination models' - po both source and destination models and mapping models, everything looks like it should. I'm about to give up and willing to loose the data in that Report entity.. Is there any way of doing it ?
So, just to try things out, i've rollback my model where it was before all this madness, created new version in which i've made only one change - dropped one field. And then i've created mapping model and try to use it. - Same error Mismatch between mapping and source/destination models - it looks like generated mapping model is bad somehow, but looking at it - i didn't see any issues.
Migrations happen in three steps: First, Core Data copies over all the objects from one data store to the next. Next, Core Data connects and relates all the objects according to the relationship mapping. Finally, enforce any data validations in the destination model.
NSPersistentContainer simplifies the creation and management of the Core Data stack by handling the creation of the managed object model ( NSManagedObjectModel ), persistent store coordinator ( NSPersistentStoreCoordinator ), and the managed object context ( NSManagedObjectContext ).
NSManagedObjectModel represents your application model file describing your app's types, properties, and relationships. NSManagedObjectContext tracks changes to instances of your application types. NSPersistentStoreCoordinator used to saves and fetch instances of your application types from stores.
We perform heavyweight migration when we have either normalized or generalized our database. In such cases, lightweight migration won't help us. We will have to manually map attributes of the old data model to the new data model, and doing so is called heavyweight migration.
If you didn't find answer i can suggest small trick. First, idk why this happens but source model, destination model and mapping model have different values of versionHashes for same entities.
i programmaticaly corrected them and migration happens.
NSURL *fileURL = [[NSBundle mainBundle] URLForResource:@"Model" withExtension:@"cdm"];
NSMappingModel *mappingModel = [[NSMappingModel alloc] initWithContentsOfURL:fileURL];
NSArray *newEntityMappings = [NSArray arrayWithArray:mappingModel.entityMappings];
for (NSEntityMapping *entityMapping in newEntityMappings) {
[entityMapping setSourceEntityVersionHash:[sourceModel.entityVersionHashesByName valueForKey:entityMapping.sourceEntityName]];
[entityMapping setDestinationEntityVersionHash:[destinationModel.entityVersionHashesByName valueForKey:entityMapping.destinationEntityName]];
}
mappingModel.entityMappings = newEntityMappings;
BOOL ok = [migrationManager migrateStoreFromURL:sourceStoreURL
type:sourceStoreType
options:nil
withMappingModel:mappingModel
toDestinationURL:destinationStoreURL
destinationType:destinationStoreType
destinationOptions:nil
error:&error];
With the help i got from this question i got to solve your issue.
I have put together a demo app based on the default master/detail template with core data. When run it first time, make sure you have the "TestData" model selected in "TestData.xcdatamodeld". Add some rows, then move to "TestData 2.xcdatamodel" and the migration will make the proper changes.
The demo app can be downloaded from here
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With