我必须在第一个版本中启用版本控制才能在后续版本中使用轻量级迁移吗?

发布于 2024-11-07 17:27:11 字数 263 浏览 0 评论 0原文

我担心的是后续版本中数据模型的变化。

我在 Xcode 中创建了一个新的 xcdatamodel 文件,默认情况下该文件没有版本控制。我知道你可以点击某个地方并将其设为“第一个版本”。在“组和文件”树中,xcdatamodel 文件左侧有一个黑色粗箭头,您可以单击该箭头查看内部的所有版本。

我的文件没有那个粗箭头,因此没有版本控制。

这会导致以后出现大问题吗?是否需要从一开始就对其进行版本控制,以便以后能够进行轻量级迁移?一旦应用程序交付给用户,就无法再更改。

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

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.

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

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

发布评论

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

评论(1

闻呓 2024-11-14 17:27:11

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

__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];

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

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.

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