簡體   English   中英

將屬性類型從Int16更改為Int64時是否需要核心數據模型遷移

[英]Is Core Data Model Migration Required when changing Attribute Type from Int16 to Int64

我已將數據模型的屬性類型從Int16更改為Int64。 是否需要遷移,否則它將自動作為相同的數據類型Int工作。 請指導。

是的,您可以更改屬性類型並在核心數據中遷移數據存儲,但在創建/配置NSPersistentStoreCoordinator您需要設置一些我在下面提到的選項。 這是我們在這里做的core-data中的LightWeight Migration

使用以下代碼更新persistentStoreCoordinator初始化方法。

- (NSPersistentStoreCoordinator *)persistentStoreCoordinator {
    // The persistent store coordinator for the application. This implementation creates and return a coordinator, having added the store for the application to it.
    if (_persistentStoreCoordinator != nil) {
        return _persistentStoreCoordinator;
    }

    // Create the coordinator and store
    _persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:[self managedObjectModel]];

    // *** add support for light weight migration ***
    NSDictionary *options = [NSDictionary dictionaryWithObjectsAndKeys:
                             [NSNumber numberWithBool:YES], NSMigratePersistentStoresAutomaticallyOption,
                             [NSNumber numberWithBool:YES], NSInferMappingModelAutomaticallyOption,
                             nil];

    NSURL *storeURL = [[self applicationDocumentsDirectory] URLByAppendingPathComponent:@"StoreName.sqlite"];
    NSError *error = nil;
// *** Add support for lightweight migration by passing options value ***
    if (![_persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeURL options:options error:&error]) {

        // Replace this with code to handle the error appropriately.
        // abort() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development.
        NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
    }

    return _persistentStoreCoordinator;
}

您可以在以下站點上閱讀有關核心數據遷移的更多信息。

1. Apple官方文檔

2.其他網站

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM