首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >iPhone核心数据“自动轻量级迁移”

iPhone核心数据“自动轻量级迁移”
EN

Stack Overflow用户
提问于 2009-12-02 09:13:27
回答 9查看 23.8K关注 0票数 64

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

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

- (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

商店25633:207错误:错误

Domain=NSCocoaErrorDomain Code=134130

UserInfo=0x1624d20“操作无法完成。(Cocoa 134130.)“2009-12-01 20:04:22.879 Shoppee25633:22.879 Unresolved error Domain=NSCocoaErrorDomain Code=134130 UserInfo=0x1624d20”操作无法完成。NSStoreModelVersionHashesVersion = 3;NSStoreModelVersionIdentifiers =( );NSStoreType = SQLite;NSStoreUUID = "8DC65301-3BC5-42BE-80B8-E44577B8F8E1";};原因=“找不到源店模型”;}

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

EN

回答 9

Stack Overflow用户

回答已采纳

发布于 2011-12-03 07:00:15

重述/完整指南:

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

在Xcode4中:选择您的.xcdatamodel ->编辑器-> Add Model Version。

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

您将看到在您的.xcdatamodeld文件夹中创建了一个新的模式(如果您已经创建了您的新模式,并根据the documentation.

  • Save.

  • Set将当前/活动模式更改为新创建的模式,则也会创建新的.xcdatamodel

在选择.xcdatamodeld文件夹的情况下:

在Xcode4:实用工具侧栏->文件检查器-> Versioned核心数据模型->中,选择新模式。

在Xcode3中: Design > Data Model > Set Current Version。

.xcdatamodel图标上的绿色标记将移动到新的schema.

  • Save.

  • Implement,这是在运行时执行迁移所需的代码。

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

[NSDictionary dictionaryWithObjectsAndKeys: NSNumber numberWithBool:YES,NSMigratePersistentStoresAutomaticallyOption,NSNumber numberWithBool:YES,NSInferMappingModelAutomaticallyOption,nil]

  • 运行你的应用程序。如果没有崩溃,您可能已经成功迁移:)

  • 成功迁移后,可以删除迁移代码(步骤7)。(应由开发人员确定发布的应用程序的用户何时可以被视为已迁移。)

要点:不会删除旧的模型版本/模式。核心数据需要旧版本迁移到新版本。

票数 144
EN

Stack Overflow用户

发布于 2009-12-02 09:35:48

我想通了。

设计>数据模型>添加模型版本

票数 26
EN

Stack Overflow用户

发布于 2010-06-28 04:34:51

对于Googler,这是你需要做的(假设你已经设置了轻量级迁移):

在进行更改之前,请设计应用程序数据模型并添加模型版本(您将看到在您的应用程序folder)

  • Save

  • Make change
  1. Save
  2. Run

中创建了一个新的应用程序

第1步是实现这一目标的关键。我遇到这个问题是因为我按照以下步骤添加了一个新字段。这招奏效了。我添加了第二个新字段,但忘记了“添加模型版本”,然后事情就爆炸了。

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

https://stackoverflow.com/questions/1830079

复制
相关文章

相似问题

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