将属性类型从 Int16 更改为 Int64 时是否需要核心数据模型迁移

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

我已将数据模型的属性类型从 Int16 更改为 Int64。它是否需要迁移,或者它会自动作为相同的数据类型 Int 工作。请指导。

是的,您可以更改属性类型并在核心数据中迁移您的数据存储区,但为此 creating/configuring 您的 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 official documentation

2. Other site