2016-04-29 76 views
2

我正在使用映射模型遷移數據模型。核心數據重命名屬性「已刪除」

實體有一個名爲屬性刪除未遷移,因爲核心數據採取刪除NSManagedObject屬性,而不是我的。

如何強制映射模型使用我的屬性?

有什麼我可以用在價值表達?這是我現在使用:

enter image description here

謝謝。

+0

我會嘗試'isDeleted'或'getDeleted' - 雖然我擔心他們會有同樣的問題(如果他們工作的話)。 – pbasdf

+0

請參閱:http://stackoverflow.com/a/16003894/826716和這裏:http://stackoverflow.com/a/4718806/826716。恐怕輕量級遷移是不可能的。也許值得一試'NSMigrationManager':https://www.objc.io/issues/4-core-data/core-data-migration/。 – bteapot

+0

我甚至不會走沉重的遷移路線,遷移後進行輕量級遷移和數據處理。 –

回答

0

我已經找到了解決辦法:

我實現了自定義MigrationPolicy如下:

- (BOOL)createDestinationInstancesForSourceInstance:(NSManagedObject *)sourceInstance 
            entityMapping:(NSEntityMapping *)mapping 
             manager:(NSMigrationManager *)manager 
              error:(NSError *__autoreleasing *)error 
{ 
    NSManagedObject *newObject = [NSEntityDescription insertNewObjectForEntityForName:[mapping destinationEntityName] inManagedObjectContext:[manager destinationContext]]; 

    // Add the old 'deleted' attribute to a renamed attribute 
    [newObject setValue:[NSNumber numberWithBool:[((OldEntityModel *)sourceInstance) deleted]] forKey:@"newDeletedAttribute"]; 

    // Add all the other attributes 
    [newObject setValue:[sourceInstance valueForKey:@"field1"] forKey:@"field1"]; 
    [newObject setValue:[sourceInstance valueForKey:@"field2"] forKey:@"field2"]; 

    // Add the relationships 
    NSSet *relationshipAttribute = [[NSSet alloc] initWithArray:[manager destinationInstancesForEntityMappingNamed:@"OtherEntityToOtherEntity" sourceInstances:@[[sourceInstance valueForKey:@"relationshipAttribute"]]]]; 
    [newObject relationshipAttribute forKey:@"relationshipAttribute"]; 

    [manager associateSourceInstance:sourceInstance withDestinationInstance:newObject forEntityMapping:mapping]; 

    return YES; 
} 

鑄造sourceEntity老款車型版本允許訪問刪除歸因這在輕量級遷移或映射模型中無法訪問。

1

不幸的是,你使用了一個保留字(我懷疑當時產生了警告)。

最好的辦法是做一個輕量級的遷移,該值將不是遷移。然後在遷移之後;迭代數據並手動更新每個對象的值。一旦遷移完成後,您只需要執行一次舊的保留字屬性就會消失。

+0

謝謝,但是如果我在輕量級遷移期間丟失了該值,如何更新該屬性? – Matt39

+0

您創建一個新的屬性,而不刪除舊的然後遷移。遷移後,遍歷對象並從舊值中設置新值。在將來某個時候,您可以刪除舊的,現在未使用的值。 –