我需要做什么才能让 Core Data 自动迁移模型? [英] What do I have to do to get Core Data to automatically migrate models?

查看:29
本文介绍了我需要做什么才能让 Core Data 自动迁移模型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已阅读有关 Core Data 模型的自动/轻量级迁移的文档 - 但我在实现它时遇到了问题.

I have read the documentation about automatic /lightweight migration for Core Data models - but I am having problems implementing it.

据我所知,应用程序应该注意到它拥有的模型和设备上已经存在的模型不同.如果您只添加了属性或关系以及类似的简单更改,那么模型应该会自动升级.

As I understand it the application should notice that the model it has and the model that exists on a device already are not the same. If you have only added attributes or relationships and similar simple changes then the model should be upgraded automatically.

任何指针 - 我需要在 Xcode 中设置一些东西吗?

Any pointers - do I need to set something in Xcode?

推荐答案

我现在发现这很简单 - 只要你知道去哪里找.

I've now found out that this is quite simple - once you know where to look.

在我的 AppDelegate 中,我设置了 NSPersistentStoreCoordinator - 您需要为此添加一些选项以告诉它处理自动迁移:

In my AppDelegate I set-up the NSPersistentStoreCoordinator - and you need to add some options to this to tell it to handle auto-migrate:

NSDictionary *options = [NSDictionary dictionaryWithObjectsAndKeys:

[NSNumber numberWithBool:YES], NSMigratePersistentStoresAutomaticallyOption,

[NSNumber numberWithBool:YES], NSInferMappingModelAutomaticallyOption, nil];

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

if (![_persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeUrl options:options error:&error]) {
    // Handle error
    NSLog(@"Problem with PersistentStoreCoordinator: %@",error);
}

那么你需要在 xCode 中做一个小技巧:

Then you need to do a little trick in xCode:

  1. 选择您的 xcdatamodel 文件
  2. 选择顶部的设计菜单 - 然后选择数据模型 - 然后选择添加模型版本
  3. 然后,您的 xcdatamodel 文件将移动到与 xcdatamodel 文件同名但扩展名为 xcdatamodeld 的新目录中 - 此目录中将有第二个文件,名称中带有 2.选择新文件,然后 Design->Data Model->Set Current Version (在 Xcode 4 中你这样做)
  4. 如果您已经进行了导致项目不兼容的更改 - 从原始 xcdatamodel 文件中删除这些更改.如果您尚未进行更改 - 只需编辑 2.xcdatamodel 文件(您刚刚制作的当前版本).
  5. 现在,当您将此版本安装到具有旧型号的设备上时 - 它会自动将该型号升级到新型号.

这看起来很棒而且和我想要的一样简单 - 但我认为在更改模型时您需要在开发过程中小心 - 否则您将不得不为每次更改创建一个新版本.

This seems great and as simple as I wanted - but I think you need to be careful during development as you change a model - otherwise you will have to create a new version for each change.

我想我要做的是保留所有更改的文件,然后一旦我准备好部署我的更新,我将删除所有中间文件,只部署最旧和最新的模型.

I think what I will do is that I will keep all of the changed files and then once I get ready to deploy my update I'll delete all the in-between files and just deploy with the oldest and latest models.

更新(15/07/2011):

UPDATE (15/07/2011):

感谢@rockstarberlin 指出 Apple 有更新的文档:

Thanks to @rockstarberlin for pointing out there is updated documentation at apple:

Xcode 4:设置托管对象模型当前版本

更新:8/19/2013 更好的链接:

Update: 8/19/2013 better link:

https://developer.apple.com/library/content/documentation/Cocoa/Conceptual/CoreDataVersioning/Articles/vmModelFormat.html

这篇关于我需要做什么才能让 Core Data 自动迁移模型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆