如果我理解您的問題(您正在尋找更新已通過App Store部署的模型),是的,您可以在Xcode中使用.xcdatamodeld格式對現有模型進行升級。來自Apple的Core Data Versioning doc全面涵蓋此主題。
這可能是一個繁瑣的過程,如果您的模型中存儲了寶貴的用戶數據,那麼您需要在推出更新之前詳盡地測試此過程。
向您的模型添加新版本;
- 選擇xcdatamodel文件(例如model.xcdatamodel)
- 點擊編輯>添加模型版本
- 命名新的模型版本(例如模式2),基於模型1
- 打開Utilities窗格
- 選擇「模型2」當前模型版本
則此方法添加到您的控制器類實現文件,以幫助使小變化到數據模型(改變字段類型,添加列等)。
- (NSPersistentStoreCoordinator *)persistentStoreCoordinator
{
if (_persistentStoreCoordinator != nil) {
return _persistentStoreCoordinator;
}
NSURL *storeURL = [[self applicationDocumentsDirectory] URLByAppendingPathComponent:@"Model.sqlite"];
NSError *error = nil;
// for performing minor changes to the coredata database
NSDictionary *options = [NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithBool:YES],
NSMigratePersistentStoresAutomaticallyOption,
[NSNumber numberWithBool:YES],
NSInferMappingModelAutomaticallyOption, nil];
_persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:[self managedObjectModel]];
if (![_persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeURL options:options error:&error]) {
NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
abort();
}
return _persistentStoreCoordinator;
}
此外,值得注意的是:蘋果建議你only store user relevant files in the Documents folder。 SQLite數據庫文件和類似文件應該存儲在/ Library/Application Support中。
請參閱編輯後的問題。 – Rashad
@Rashad:這取決於你的需要。在我看來,使用舊桌子將是一個好主意。 –