我正在尝试使用 Objective-C 中的轻量级迁移将 NSNumber 迁移到 NSString 属性。版本 0
第 2 版
我已将核心数据模型当前版本更改为 2
由于它不会在轻量级迁移中得到解决,我尝试使用 CoreDataMapping 模型和 NSEntity 迁移策略,如下所示。
创建了 NSEntityMigrationPolicy 的子类并尝试了以下代码
@interface SampleMigrationV1toV2 : NSEntityMigrationPolicy
@end
#import "SampleMigrationV1toV2.h"
@implementation SampleMigrationV1toV2
- (BOOL)createDestinationInstancesForSourceInstanceNSManagedObject *)sInstance entityMappingNSEntityMapping *)mapping managerNSMigrationManager *)manager errorNSError **)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
在持久协调器中,我已将 NSInferMappingModelAutomaticallyOption 更改为 NO,如下所示,
- (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 optionsptions error:&error])
{
//NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
abort();
}
return persistentStoreCoordinator;
}
做完所有这些事情后,我收到了类似“Error Domain=NSCocoaErrorDomain Code=134110”(null)"UserInfo={NSUnderlyingException=Couldn't create mapping policy for class named (CoreDataMigrationPolicty.SampleMigrationV1toV2)} with userInfo 字典之类的错误
未创建/调用映射模型。更改核心数据中的属性数据类型是否正确?有没有其他方法可以在不崩溃/重新安装应用程序的情况下解决此问题。
Best Answer-推荐答案 strong>
最后我发现我从这个答案中所做的错误。
Core Data Migration: Attribute Mapping Value Expression
问题在于我在映射模型属性中提到的表达式。
语法是 FUNCTION($entityPolicy, "convertNumberToString:", $source.number) 函数定义是这样的
#import "SampleMigrationV1toV2.h"
@implementation SampleMigrationV1toV2
-(NSString *)convertNumberToStringNSNumber *)number{
return [number stringValue];
}
@end
FUNCTION($entityPolicy, "convertNumberToString:", $source.number) :
$entityPolicy 获取我们在EntityMapping“SampleToSample”中提到的对应的MappingPolicy(即SampleMigrationV1toV2)和函数“convertNumberToString:”通过取输入值$source.Number将数字迁移到字符串
关于ios - 在 CoreData 实体中将属性从 NSNumber 转换为 NSString - LightWeightMigration,我们在Stack Overflow上找到一个类似的问题:
https://stackoverflow.com/questions/46892791/
|