首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >核心数据版本控制(轻量级迁移)问题

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

Stack Overflow用户
提问于 2015-01-28 22:04:01
回答 1查看 149关注 0票数 0

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

代码语言:javascript
复制
- (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上没有崩溃日志。

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

EN

回答 1

Stack Overflow用户

发布于 2015-01-28 22:28:53

您是否有一个新的版本模型来反映您的更改?或者您只是在一个唯一的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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/28194209

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档