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}, {
EDIT 2
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 ?
EDIT 3
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.