核心数据的多个版本之间的迁移

发布于 2024-10-04 21:42:10 字数 1394 浏览 0 评论 0原文

我有核心数据应用程序,并且一直在迁移(升级)核心数据模型。每次创建新版本时,我都会为每个版本创建一个映射模型。 现在我有 16 个版本,并且我的映射模型如下所示: 1to2.xc映射模型 2to3.xcmapping模型 3to4.xcmapping模型 ...ETC。最多 16

这工作正常,但是当一个用户拥有版本 10 的数据文件并更新版本 16 的应用程序时,就会出现问题。我认为 Core Data 会自动从 10 升级到 16,但显示错误说“缺少映射模型”。为了确保映射模型正确,我将其一一升级到每个版本(10 到 11、11 到 12 等..)并且它确实有效...... 这是我的代码。

我使用此代码指定模型版本:

NSBundle *modelWrapper = [NSBundle bundleWithPath:[[NSBundle mainBundle] pathForResource:@"TaskApp_DataModel" ofType:@"momd"]];
NSString *modelPath = [modelWrapper pathForResource:@"TaskApp_DataModel 16" ofType:@"mom"];
NSLog(@"%@",modelPath);
managedObjectModel = [[NSManagedObjectModel alloc]initWithContentsOfURL:[NSURL fileURLWithPath:modelPath]];

并且我在此处定义“自动迁移”选项:

  NSURL *url = [NSURL fileURLWithPath: [applicationSupportDirectory stringByAppendingPathComponent: @"storedata-sql"]];


NSMutableDictionary *dict = [NSMutableDictionary dictionary];
[dict setObject:[NSNumber numberWithBool:YES] forKey:NSMigratePersistentStoresAutomaticallyOption];



if (![persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType 
                                            configuration:nil 
                                            URL:url 
                                            options:dict 
                                            error:&error]){

有人知道如何升级它吗? 谢谢。

I have Core Data application and I have been migrating (upgrading) the core data model. Each time I create a new version I create a mapping model for each version.
Right now I have 16 versions and I have mapping models that go like this:
1to2.xcmappingmodel
2to3.xcmappingmodel
3to4.xcmappingmodel
...etc. up to 16

This works fine, but a problem arises when one user has a data file with version 10 and updates the application that has version 16. Some how I thought Core Data will automatically upgrade from 10 to 16, but an error shows up that says "Missing Mapping Model". To make sure the mapping models where correct, I upgrade it to each version one by one (10 to 11, 11 to 12, etc..) and it did work...
Here is my code.

I specify the model version with this code:

NSBundle *modelWrapper = [NSBundle bundleWithPath:[[NSBundle mainBundle] pathForResource:@"TaskApp_DataModel" ofType:@"momd"]];
NSString *modelPath = [modelWrapper pathForResource:@"TaskApp_DataModel 16" ofType:@"mom"];
NSLog(@"%@",modelPath);
managedObjectModel = [[NSManagedObjectModel alloc]initWithContentsOfURL:[NSURL fileURLWithPath:modelPath]];

And I define the Migrate Automatic option here:

  NSURL *url = [NSURL fileURLWithPath: [applicationSupportDirectory stringByAppendingPathComponent: @"storedata-sql"]];


NSMutableDictionary *dict = [NSMutableDictionary dictionary];
[dict setObject:[NSNumber numberWithBool:YES] forKey:NSMigratePersistentStoresAutomaticallyOption];



if (![persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType 
                                            configuration:nil 
                                            URL:url 
                                            options:dict 
                                            error:&error]){

Does anyone know how to upgrade this?
Thank you.

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

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

发布评论

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

评论(1

北方的巷 2024-10-11 21:42:11

它只会尝试从用户现有版本(可能是 v1)转到当前版本。如果您有 3 个版本,那么您需要 v1-v2、v2-v3、v1-v3 的映射。 16个版本都发货了吗?
如果是这样,您可能需要开始制作新的迁移地图,如果您以前没有尝试过,那么启用自动迁移也可能是值得的,因为它可以很好地填补空白。我认为是:

[dict setObject:[NSNumber numberWithBool:YES] forKey:NSInfersMappingModel];

但您必须仔细检查。

很抱歉带来坏消息

it will only try to go from the users existing version(possibly v1), to the current version. if you have 3 versions then you need maps for v1-v2,v2-v3,v1-v3. have all of the 16 versions shipped?
if so you may need to start making new migration maps, it may also be worth enabling automatic migration if you haven't tried it before, as it can be very good at filling in the gaps. i think it is:

[dict setObject:[NSNumber numberWithBool:YES] forKey:NSInfersMappingModel];

but you will have to double check that.

sorry to be the bringer of bad news

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