ios - iPhone 核心数据 "Automatic Lightweight Migration"

标签 ios iphone core-data core-data-migration

我正在尝试更新实现核心数据存储的应用程序。我正在向其中一个实体添加一个属性。

我将以下代码添加到我的委托(delegate)类中:

- (NSPersistentStoreCoordinator *)persistentStoreCoordinator {

    if (persistentStoreCoordinator != nil) {
        return persistentStoreCoordinator;
    }

    NSURL *storeUrl = [NSURL fileURLWithPath: [[self applicationDocumentsDirectory] stringByAppendingPathComponent: @"Shoppee.sqlite"]];

    NSError *error = nil;
    persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:[self managedObjectModel]];

    NSDictionary *options = [NSDictionary dictionaryWithObjectsAndKeys:
                             [NSNumber numberWithBool:YES], NSMigratePersistentStoresAutomaticallyOption,
                             [NSNumber numberWithBool:YES], NSInferMappingModelAutomaticallyOption, nil];

    if (![persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeUrl options:options error:&error]) {
        NSLog(@"Error: %@",error);
        NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
        abort();
    }    

    return persistentStoreCoordinator;
}

这是来自以下网址: Doc

执行代码时出现以下错误:

2009-12-01 20:04:22.877

Shoppee[25633:207] Error: Error

Domain=NSCocoaErrorDomain Code=134130

UserInfo=0x1624d20 "Operation could not be completed. (Cocoa error 134130.)" 2009-12-01 20:04:22.879 Shoppee[25633:207] Unresolved error Error Domain=NSCocoaErrorDomain Code=134130 UserInfo=0x1624d20 "Operation could not be completed. (Cocoa error 134130.)", { URL = file://localhost/Users/Eric/Library/Application%20Support/iPhone%20Simulator/User/Applications/A8A8FB73-9AB9-4EB7-8F83-82F5B4467AF1/Documents/MyApp.sqlite; metadata = { NSPersistenceFrameworkVersion = 241; NSStoreModelVersionHashes = { Item = <869d4b20 088e5c44 5c345006 87d245cd 67ab9bc4 14cadf45 180251e9 f741a98f>; Store = <47c250f4 895e6fd1 5033ab42 22d2d493 7819ba75 3c0acffc 2dc54515 8deeed7a>; }; NSStoreModelVersionHashesVersion = 3; NSStoreModelVersionIdentifiers = ( ); NSStoreType = SQLite; NSStoreUUID = "8DC65301-3BC5-42BE-80B8-E44577B8F8E1"; }; reason = "Can't find model for source store"; }

看起来我需要以某种方式包含原始数据模型,但我不确定该怎么做。有什么建议吗?

最佳答案

回顾/完整指南:

  1. 在进行任何更改之前,创建一个新的模型版本。

    在 Xcode 4 中:选择您的 .xcdatamodel -> Editor -> Add Model Version。

    在 Xcode 3 中:设计 -> 数据模型 -> 添加模型版本。

    您会看到在您的 .xcdatamodeld 文件夹中创建了一个新的 .xcdatamodel(如果您有无)

  2. 保存。

  3. 选择您的新 .xcdatamodel 并根据 the Lightweight Migration documentation 进行您希望采用的更改.

  4. 保存。

  5. 将当前/事件模式设置为新创建的模式。

    选择 .xcdatamodeld 文件夹:

    在 Xcode 4 中:Utilities sidebar -> File Inspector -> Versioned Core Data Model -> 选择新模式。

    在 Xcode 3 中:设计 > 数据模型 > 设置当前版本。

    .xcdatamodel 图标上的绿色勾号将移动到新架构。

  6. 保存。

  7. 实现必要的代码以在运行时执行迁移。

    在创建 NSPersistentStoreCoordinator 的地方(通常是 AppDelegate 类),对于 options 参数,将 nil 替换为以下代码:

    [NSDictionary dictionaryWithObjectsAndKeys:
                      [NSNumber numberWithBool:YES], NSMigratePersistentStoresAutomaticallyOption, 
                      [NSNumber numberWithBool:YES], NSInferMappingModelAutomaticallyOption, nil]
    
  8. 运行您的应用。如果没有崩溃,您可能已经成功迁移:)

  9. 成功迁移后,可以删除迁移代码(第 7 步)。 (由开发者决定已发布应用的用户何时可以被视为已迁移。)

重要提示:请勿删除旧模型版本/架构。 Core Data 需要旧版本迁移到新版本。

关于ios - iPhone 核心数据 "Automatic Lightweight Migration",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1830079/

相关文章:

ios - 如何将信息从自定义单元格发送到 View Controller ?

iphone - 检查 MBProgressHUD 状态是否隐藏/显示

objective-c - committedValuesForKeys 在 NSManaged 对象子类上返回一个空字典

objective-c - 将 NSString 转换为十六进制

ios - CoreData 在插入和删除同时发生时崩溃

ios - NSUserDefaults 以外的本地键值存储

android - 2d 手机游戏

ios - 无法滚动到 UIScrollView 中嵌入的 UITableView 的顶部

ios - 将数据加载到 Metal 中的 3D 纹理中

ios - 将相机胶卷照片获取到自定义收藏 View