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

发布于 2024-08-12 15:53:39 字数 2481 浏览 1 评论 0原文

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

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

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

这来自以下 URL: Doc

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

2009-12-01 20:04:22.877

Shoppee[25633:207] 错误:错误

域=NSCocoaErrorDomain代码=134130

UserInfo=0x1624d20“操作无法完成。(可可错误 134130.)" 2009-12-01 20:04:22.879 Shoppee[25633:207] 未解决的错误 错误域=NSCocoaErrorDomain 代码=134130 UserInfo=0x1624d20 “操作无法完成。(Cocoa 错误 134130。)”,{ URL = 文件://localhost/Users/Eric/Library/Application%20Support/iPhone%20Simulator/User/Applications/A8A8FB73-9AB9-4EB7-8F83-82F5B4467AF1/Documents/MyApp.sqlite; 元数据={ NSPersistenceFrameworkVersion = 241; NSStoreModelVersionHashes = { 项目 = <869d4b20 088e5c44 5c345006 87d245cd 67ab9bc4 14cadf45 180251e9 f741a98f>; 存储 = <47c250f4 895e6fd1 5033ab42 22d2d493 7819ba75 3c0acffc 2dc54515 8deeed7a>; }; NSStoreModelVersionHashesVersion = 3; NSStoreModelVersionIdentifiers = ( ); NSStoreType = SQLite; NSStoreUUID = "8DC65301-3BC5-42BE-80B8-E44577B8F8E1"; }; Reason = "找不到源商店的模型"; }

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

I am attempting to update an app that implements a core data store. I am adding an attribute to one of the entities.

I added the following code to my delegate class:

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

This was from the following URL:
Doc

I get the following error when executing the code:

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"; }

It looks like I somehow need to include the original data model but I am not sure how to do that. Any suggestions?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(9

凉城 2024-08-19 15:53:39

回顾/完整指南:

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

    在 Xcode 4 中:选择您的 .xcdatamodel ->编辑->添加模型版本。

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

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

  2. 保存。

  3. 选择新的 .xcdatamodel 并根据 轻量级迁移文档

  4. 保存。

  5. 将当前/活动架构设置为新创建的架构。

    选择.xcdatamodeld文件夹后:

    在 Xcode 4 中:实用工具侧边栏 ->文件检查器 ->版本化核心数据模型 ->选择新架构。

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

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

  6. 保存。

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

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

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

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

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

To recap/Full guide:

  1. Before making any change, create a new model version.

    In Xcode 4: Select your .xcdatamodel -> Editor -> Add Model Version.

    In Xcode 3: Design -> Data Model -> Add Model Version.

    You will see that a new .xcdatamodel is created in your .xcdatamodeld folder (which is also created if you have none).

  2. Save.

  3. Select your new .xcdatamodel and make the change you wish to employ in accordance with the Lightweight Migration documentation.

  4. Save.

  5. Set the current/active schema to the newly created schema.

    With the .xcdatamodeld folder selected:

    In Xcode 4: Utilities sidebar -> File Inspector -> Versioned Core Data Model -> Select the new schema.

    In Xcode 3: Design > Data Model > Set Current Version.

    The green tick on the .xcdatamodel icon will move to the new schema.

  6. Save.

  7. Implement the necessary code to perform migration at runtime.

    Where your NSPersistentStoreCoordinator is created (usually AppDelegate class), for the options parameter, replace nil with the following code:

    [NSDictionary dictionaryWithObjectsAndKeys:
                      [NSNumber numberWithBool:YES], NSMigratePersistentStoresAutomaticallyOption, 
                      [NSNumber numberWithBool:YES], NSInferMappingModelAutomaticallyOption, nil]
    
  8. Run your app. If there's no crash, you've probably successfully migrated :)

  9. When you have successfully migrated, the migration code (step 7) can be removed. (It is up to the developer to determine when the users of a published app can be deemed to have migrated.)

IMPORTANT: Do not delete old model versions/schemas. Core Data needs the old version to migrate to the new version.

时光病人 2024-08-19 15:53:39

我想通了。

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

I figured it out.

Design > Data Model > Add Model Version

感情旳空白 2024-08-19 15:53:39

对于 Google 员工来说,这就是您需要做的(假设您已经设置了轻量级迁移):

  1. 在进行更改之前,进行设计 -> 进行更改。数据模型->添加模型版本(您将看到在 .xcdatamodeld 文件夹中创建了一个新的 .xcdatamodel
  2. 保存
  3. 进行更改
  4. 保存
  5. 运行应用程序

步骤 # 1 对于这项工作至关重要。我遇到了这个问题,因为我按照以下步骤添加了新字段。那行得通。我添加了第二个新字段,但忘记了“添加模型版本”,事情就爆发了。

For Googlers again, this is what you need to do (assuming you have already set up Lightweight Migration):

  1. Before making changes, Do Design -> Data Model -> Add Model Version (you will see that a new .xcdatamodel is created in your .xcdatamodeld folder)
  2. Save
  3. Make your change
  4. Save
  5. Run App

Step #1 is crucial for making this work. I ran into this problem because I had followed these steps to add a new field. That worked. I added a second new field, but forgot to "Add Model Version", and things blew up.

渡你暖光 2024-08-19 15:53:39

也适用于谷歌用户......简单的规则,永远不要删除/编辑任何旧的编号版本。当您添加模型版本时,数字后缀将增加为 2..3..4,这意味着 2 是最旧的 3 个,依此类推。但当前要编辑的版本是未编号的版本。

不要删除旧模型版本,因为使用旧模型版本的先前数据库的用户将无法迁移到最新的数据库模型,而不比较旧的和最新的架构。

Also for googlers.. Simple rule, never delete/edit any old numbered version. When you Add Model Version the number suffix will increase as 2..3..4 meaning 2 is the oldest 3 next etc.. but the Current one to edit is the unnumbered version.

Do not delete old model versions as users with previous db using an old model version will not be able to migrate to your latest db model with out comparing old and latest schemas.

堇年纸鸢 2024-08-19 15:53:39

对于那些遇到此谷歌搜索的人来说,请注意,即使使用自动(神奇)迁移,您仍然需要创建原始商店的一个版本和一个新版本,并将新版本设置为当前版本。

Just a note for those that come across this Googling, it seems even with auto(magic) migration you still need to create a version of your original store, and a new one, and set the new one as the current version.

扎心 2024-08-19 15:53:39

到目前为止我只看到如何避免错误消息。

但我们该如何解决它——万一我们已经把事情搞砸了呢?

以下解决方案解决了问题,但您将丢失数据库中的数据

删除/重命名已部署/安装的应用程序的 sqlite 文件
文件名和位置直接在错误消息之后给出。例如:

reason=找不到源商店的模型},{
URL =“文件://localhost/Users/yourName/Library/Application%20Support/iPhone%20Simulator/4.3/Applications/62F342D4-F007-4F6F-96D2-68F902D3719A/Documents/Locations.sqlite” ;

So far I only see how to avoid the error message.

But how do we fix it - in case we messed things up already??

The following solution fixed the problem but you will loose the data in the DB:

Delete / rename the sqlite file of the deployed / installed application.
The files name an location are given directly after the error message. e.g.:

reason=Can't find model for source store}, {
URL = "file://localhost/Users/yourName/Library/Application%20Support/iPhone%20Simulator/4.3/Applications/62F342D4-F007-4F6F-96D2-68F902D3719A/Documents/Locations.sqlite";

各自安好 2024-08-19 15:53:39

进行轻量级迁移时要记住的事情 -

如果您计划重命名/修改属性,请记住在新模型或旧模型中设置“重命名 ID”值。要使用Apple自己的示例,在XCode 4.3中,在新模型>中选择paintColor切换到数据模型检查器>在“版本控制”部分中将“重命名 ID”字段设置为“颜色”。对我来说,未能执行此步骤会导致运行时错误。 此处也介绍了同样的错误。作为新用户,我不允许发布图片,所以这里有一个 imgur 链接 (不是垃圾邮件,真的)。

    (Cocoa error 134140.)" UserInfo=0x622b350 {reason=Can't find or automatically infer mapping model for migration

Something to keep in mind when doing a lightweight migration -

If you plan to rename/modify attributes, remember to set the "Renaming ID" value in either the new or the old model. To use Apple's own example, in XCode 4.3, select paintColor in the new model > switch to the Data Model Inspector > Set the "Renaming ID" field to Color in the "Versioning" section. For me, failure to do this step led to a run time error. This same error is also covered here. As a new user, I'm not allowed to post images, so here's an imgur link (not spam, really).

    (Cocoa error 134140.)" UserInfo=0x622b350 {reason=Can't find or automatically infer mapping model for migration
那请放手 2024-08-19 15:53:39

当更改数据模型并在具有不同版本的 sqlite 文件的已安装应用程序上运行时,您也可能会收到此错误。在这种情况下,只需删除已安装的应用程序并重新运行它即可。

You can also get this error when making a change to the data model and running on an installed app that has a different version of the sqlite file. In this case just delete the installed app and re-run it.

岁月流歌 2024-08-19 15:53:39

以防万一有人遇到这种情况,并且上述方法都不起作用...我正在从模拟器中删除我的应用程序,进行清理等,但没有任何效果。我必须转到模拟器目录并手动 rm .sqlite 文件才能使应用程序再次运行。没有任何线索...

Just in case someone runs into this scenario and none of the above works... I was deleting my app from the simulator, cleaning, etc, but nothing would work. I had to go to the simulator directory and manually rm the .sqlite file to get the app working again. No clue...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文