简体   繁体   中英

Convert attribute from NSNumber to NSString in CoreData Entity - LightWeightMigration

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.

Finally I found mistake what I did from this answer. Core Data Migration: Attribute Mapping Value Expression

The problem is with the expression I mentioned in the mapping model attribute. Syntax is FUNCTION($entityPolicy, "convertNumberToString:" , $source.number) and the function definition is like

#import "SampleMigrationV1toV2.h"

@implementation SampleMigrationV1toV2

-(NSString *)convertNumberToString:(NSNumber *)number{
    return  [number stringValue];
}
@end

在点击的实体映射SampleToSample时,提及自定义策略名称(即SampleMigrationV1toV2)

单击要迁移的属性后,输入表达式“ FUNCTION($ entityPolicy,“ convertNumberToString:”,$ source.number)“

FUNCTION($entityPolicy, "convertNumberToString:" , $source.number) :

$entityPolicy get the corresponding MappingPolicy (ie SampleMigrationV1toV2) as we mentioned in the EntityMapping "SampleToSample" and the function "convertNumberToString:" migrate the number to string by taking the input value $source.Number

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM