![](/img/trans.png)
[英]Int16 backed enum attribute in Core Data entity appears to have incorrect value when passed to a model migration function in a custom migration policy
[英]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;
}
您可以在以下站點上閱讀有關核心數據遷移的更多信息。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.