I'm trying migrate NSNumber to NSString attribute using lightweight migration in objective C. Version 0
Version 2
I've changed the core data model current version to 2
Since it won't resolved in light weight migration I tried with CoreDataMapping Model and NSEntity migration policy like below.
Have created a subclass of NSEntityMigrationPolicy and tried the below code
@interface SampleMigrationV1toV2 : NSEntityMigrationPolicy
@end
#import "SampleMigrationV1toV2.h"
@implementation SampleMigrationV1toV2
- (BOOL)createDestinationInstancesForSourceInstance:(NSManagedObject *)sInstance entityMapping:(NSEntityMapping *)mapping manager:(NSMigrationManager *)manager error:(NSError **)error
{
NSManagedObject *newObject = [NSEntityDescription insertNewObjectForEntityForName:[mapping destinationEntityName] inManagedObjectContext:[manager destinationContext]];
// Copy all the values from sInstance into newObject, making sure to apply the conversion for the string to int when appropriate. So you should have one of these for each attribute:
[newObject setValue:[sInstance valueForKey:@"number"] forKey:@"number"];
[manager associateSourceInstance:sInstance withDestinationInstance:newObject forEntityMapping:mapping];
return YES;
}
@end
In persistent coordinator I've changed the NSInferMappingModelAutomaticallyOption to NO like below,
- (NSPersistentStoreCoordinator *)persistentStoreCoordinator {
if (persistentStoreCoordinator != nil) {
return persistentStoreCoordinator;
}
NSString *storePath = [[self applicationDocumentsDirectory] stringByAppendingPathComponent:@"CoreDataMigrationPolicty.sqlite"];
NSURL *storeUrl = [NSURL fileURLWithPath:storePath];
NSMutableDictionary *options = [[NSMutableDictionary alloc] init];
[options setObject:[NSNumber numberWithBool:YES] forKey:NSMigratePersistentStoresAutomaticallyOption];
[options setObject:[NSNumber numberWithBool:NO] forKey:NSInferMappingModelAutomaticallyOption];
NSError *error = nil;
persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:[self managedObjectModel]];
if (![persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeUrl options:options error:&error])
{
//NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
abort();
}
return persistentStoreCoordinator;
}
After doing all these thing I'm getting error like " Error Domain=NSCocoaErrorDomain Code=134110 "(null)" UserInfo={NSUnderlyingException=Couldn't create mapping policy for class named (CoreDataMigrationPolicty.SampleMigrationV1toV2)} with userInfo dictionary
Mapping model is not being created/called. Is it correct approach to change the attribute datatype in core data. Is there any other way to fix this issue without crash/reinstalling the app.