2015-10-28 89 views
0

我試圖手動調用CoreData遷移使用由xcode(.xcdatamodel)創建的數據模型和在運行時以編程方式創建的映射模型。 遷移似乎完成且沒有錯誤,但新創建的數據庫中沒有數據。數據庫的結構在那裏(表,Z_METADATA等),但數據還沒有結轉,顯然我的NSMappingModel沒有被應用。CoreData自定義NSMappingModel不遷移數據

總結遷移:Country實體已重命名爲Country2,並且Country.name已重命名爲Country2.new_name。

NSMutableArray *attrMaps = [[NSMutableArray alloc] init]; 
NSPropertyMapping *pMap = [[NSPropertyMapping alloc] init]; 
pMap.name = @"new_name"; 
pMap.valueExpression = [NSExpression expressionForKeyPath:@"source.name"]; 
[attrMaps addObject:pMap]; 

NSEntityMapping *eMap = [[NSEntityMapping alloc] init]; 
eMap.sourceEntityName = @"Country"; 
eMap.destinationEntityName = @"Country2"; 
eMap.name = @"CountryToCountry2"; 
eMap.mappingType = NSTransformEntityMappingType; 
eMap.attributeMappings = attrMaps; 

NSMutableArray *eMaps = [[NSMutableArray alloc] init]; 
[eMaps addObject:eMap]; 

NSMappingModel *mappingModel = [[NSMappingModel alloc] init]; 
mappingModel.entityMappings = eMaps; 

NSURL * storeURL = [NSURL URLWithString:[NSString stringWithFormat:@"file://%@/Documents/%@",NSHomeDirectory(),@"TestCoreData.sqlite"]]; 
NSURL * storeURL2 = [NSURL URLWithString:[NSString stringWithFormat:@"file://%@/Documents/%@",NSHomeDirectory(),@"TestCoreData2.sqlite"]]; 
NSMigrationManager *migman = [self getMigrationManager]; 

NSError * err; 
BOOL ok = [migman migrateStoreFromURL:storeURL 
           type:NSSQLiteStoreType 
           options:nil 
        withMappingModel:mappingModel 
        toDestinationURL:storeURL2 
         destinationType:NSSQLiteStoreType 
        destinationOptions:nil 
           error:&err]; 

更多信息

比較我的手工製作NSMappingModel一個在Xcode的GUI創建的,你可以看到有一些區別:

從Xcode的GUI

entityMappings (
"(NSEntityMapping), 
    name CountryToCountry2, 
    mappingType 5, 
    sourceEntityName Country, 
    sourceEntityVersionHash <828c33cf 3ffdc5df dd1e73e8 4f7b60a5 f10dcb43 af26adeb 33d2c518 0c992dd8>, 
    destinationEntityName Country2, 
    destinationEntityVersionHash <8cdb8ecf cb47b39d 4b45858f a247487a dd88088b f509a6ec cc938740 5cf7a041>, attributeMappings (
    "(NSPropertyMapping), name new_name, valueExpression $source.name, userInfo (null)" 
    ), relationshipMappings (
), sourceExpression FETCH(FUNCTION($manager, "fetchRequestForSourceEntityNamed:predicateString:" , "Country", "TRUEPREDICATE"), $manager.sourceContext, NO), 
    entityMigrationPolicyClassName (null), userInfo (null)" 

礦井

entityMappings (
"(NSEntityMapping), 
    name CountryToCountry2, 
    mappingType 5, 
    sourceEntityName Country, 
    sourceEntityVersionHash (null), 
    destinationEntityName Country2, 
    destinationEntityVersionHash (null), 
    attributeMappings (
    "(NSPropertyMapping), name new_name, valueExpression source.name, userInfo (null)", 
    ), relationshipMappings (
), sourceExpression (null), 
    entityMigrationPolicyClassName (null), userInfo (null)" 

更新

我已經添加了這個sourceExpression到我的實體映射,現在國家的行被複制到新的數據庫,但它們是空的。所以,似乎仍然在NSPropertyMappings未生效:

eMap.sourceExpression = [NSExpression expressionWithFormat:@"FETCH(FUNCTION($manager, \"fetchRequestForSourceEntityNamed:predicateString:\" , \"Country\", \"TRUEPREDICATE\"), $manager.sourceContext, NO)"]; 
+0

您的實體映射沒有版本散列。爲什麼你以編程方式創建它? – Willeke

+0

@Willeke我實際上已經知道了這一點,不知何故 - 哈希值並不重要。問題是我定義NSExpressions的方式是錯誤的。 – Matt

+0

我實際上遇到了「FETCH(FUNCTION($ manager,\」fetchRequestForSourceEntityNam「...)的問題,並且在我來到這裏之前就想通了,我在這裏用google搜索到了」fetchRequestForSourceEntityNamed「,我找不到任何真正的地方任何人都明白這一點? – netigger

回答

1

的valueExpression爲NSPropertyMapping是錯誤的定義。它應該是expressionWithFormat:而不是expressionForKeyPath:$source而不是source。像這樣:

pMap.valueExpression = [NSExpression expressionWithFormat:@"$source.name"]; 

然後,我有一個手工製作的NSMappingModel實際工作。