核心数据版本控制(轻量级迁移)问题



我在我的应用程序中使用了核心数据,它的第一个版本于 10 月发布。然后我在数据模型中进行了一些更改并上传了下一个版本。它开始在现有用户的设备上崩溃(启动时崩溃),但是当他们重新安装它或新用户下载它时,它运行正常。然后我了解了版本控制并创建了一个新版本,即 1.1,并在应用程序委托中添加了以下代码行

- (NSPersistentStoreCoordinator *)persistentStoreCoordinator{
if (_persistentStoreCoordinator != nil) {
    return _persistentStoreCoordinator;
}
NSURL *storeURL = [[self applicationDocumentsDirectory] URLByAppendingPathComponent:@"golfCourse.sqlite"];
NSError *error = nil;
_persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:[self managedObjectModel]];
NSDictionary *options = @{
                          NSMigratePersistentStoresAutomaticallyOption : @YES,
                          NSInferMappingModelAutomaticallyOption : @YES
                          };
if (![_persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeURL options:options error:&error]) {
    /*
     Replace this implementation 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.
     Typical reasons for an error here include:
     * The persistent store is not accessible;
     * The schema for the persistent store is incompatible with current managed object model.
     Check the error message to determine what the actual problem was.

     If the persistent store is not accessible, there is typically something wrong with the file path. Often, a file URL is pointing into the application's resources directory instead of a writeable directory.
     If you encounter schema incompatibility errors during development, you can reduce their frequency by:
     * Simply deleting the existing store:
     [[NSFileManager defaultManager] removeItemAtURL:storeURL error:nil]
     * Performing automatic lightweight migration by passing the following dictionary as the options parameter:
     @{NSMigratePersistentStoresAutomaticallyOption:@YES, NSInferMappingModelAutomaticallyOption:@YES}
     Lightweight migration will only work for a limited set of schema changes; consult "Core Data Model Versioning and Data Migration Programming Guide" for details.
     */
    NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
    abort();
}
return _persistentStoreCoordinator;}

我在模拟器和 iPhone 上对此进行了测试,它工作正常,但是当我上传它时。对于现有应用程序用户,它在启动时再次开始崩溃。

iTunes上没有崩溃日志。

为了测试这个问题,我创建了另一个版本的数据模型并遵循了这个测试模型版本控制的方法。它工作正常。应用程序在启动时崩溃,所以我想数据模型的变化是原因。但是我不确定为什么在添加适当的选项(轻量级迁移)后它会崩溃。

您是否有反映更改的新版本模型?或者您刚刚在一个唯一的 xcdatamodeld 文件中完成了更改?要创建新的模型版本:

  1. 选择您的 xcdatamodel 文件
  2. 编辑器 -> 添加模型版本
  3. 再次选择您的 xcdatamodel 文件,然后在右侧面板中找到模型版本并将当前设置为您刚刚创建的模型

检查 https://developer.apple.com/library/ios/documentation/Cocoa/Conceptual/CoreDataVersioning/Articles/vmModelFormat.html#//apple_ref/doc/uid/TP40004399-CH3-SW1

最新更新