为了在后续版本中使用轻量级迁移,必须在我的第一个版本中启用版本控制吗? [英] Must I enable versioning in my first release in order to use lightweight migration in subsequent releases?

查看:117
本文介绍了为了在后续版本中使用轻量级迁移,必须在我的第一个版本中启用版本控制吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的恐惧是在后续版本中的数据模型的改变。

My fear is the change of the data model in subsequent releases.

我在Xcode中创建了一个新的xcdatamodel文件,默认不进行版本控制。我知道你可以点击某处,使其成为第一个版本。在组和文件树中,xcdatamodel文件在左侧有一个粗黑色箭头,您可以点击它查看其中的所有版本。

I created a new xcdatamodel file in Xcode which is not versioned by default. I know you can click somewhere and make it "the first version". In the Groups and Files tree the xcdatamodel file gets a thick black arrow on the left side which you can click to see all the versions inside.

我的文件没有粗箭头所以没有版本。

My file does not have that thick arrow so is not versioned.

这是否会导致大问题?是否需要从一开始就进行版本化,以便稍后进行轻量级迁移工作?

Does this cause big problems later? Is it needed to version it right from the start to make lightweight migration work later? Once the app is shipped to users this can't be changed anymore.

推荐答案

您不需要设置轻量级迁移您的第一个版本。当你决定迁移时,你需要做两件事。首先,您必须保留数据模型的每个版本的副本。这些模型通常保存在.xcdatamodeld文件中。或者,你可以只保留一堆.xcdatamodel文件。但是,最好使用.xcdatamodeld文件来保持一切有序。其次,您必须使用持久存储协调器激活轻量级迁移。当创建持久化存储协调器时,你将做如下:

You do not need to set up lightweight migration in your first release. When you do decide to migrate, you need to do two things. First, you must keep a copy of each version of your data model. These models are usually held in a .xcdatamodeld file. Alternatively, you can just keep a bunch of .xcdatamodel files. However, it's best to use the .xcdatamodeld file to keep everything organized. Second, you must activate lightweight migration with your persistent store coordinator. When creating your persistent store coordinator, you will do something like the following:

__persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:[self managedObjectModel]];

// Automatically migrates the model when there are small changes.
NSDictionary * options = [NSDictionary dictionaryWithObjectsAndKeys:
                          [NSNumber numberWithBool:YES], NSMigratePersistentStoresAutomaticallyOption,
                          [NSNumber numberWithBool:YES], NSInferMappingModelAutomaticallyOption, 
                          nil];
[__persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType 
                                           configuration:nil 
                                                     URL:storeURL 
                                                 options:options 
                                                   error:&error];

请记住,轻量级迁移只能做这么多。如果您需要进行更大的更改,那么您将需要创建一个映射模型。

Remember that lightweight migration can only do so much. If you need to make heavier changes, then you will need to create a mapping model.

这篇关于为了在后续版本中使用轻量级迁移,必须在我的第一个版本中启用版本控制吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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