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

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

问题描述

我已经阅读了关于Core Data模型的自动/轻量级迁移的文档 - 但是在实现它的现实中有问题。



我理解它应用程序应该注意到它拥有的模型和存在于设备上的模型已经不一样了。如果您只添加了属性或关系和类似的简单更改,则应自动升级模型。



任何指针 - 我需要在xCode中设置某些内容吗?

解决方案

我现在发现这很简单 - 一旦你知道在哪里看。



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



<$ p $ (NSNumber numberWithBool:YES],NSMigratePersistentStoresAutomaticallyOption,

[NSNumber numberWithBool:YES],NSInferMappingModelAutomaticallyOption,NSDictionary *零];

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

if(![_ persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeUrl options:options error:& error]){
//处理错误
NSLog with PersistentStoreCoordinator:%@,error);
}

然后你需要在xCode中做一个小技巧:


  1. 选择您的xcdatamodel文件

  2. 选择顶部的设计菜单 - 然后选择数据模型 - / li>
  3. 然后,您的xcdatamodel文件将移动到与xcdatamodel文件同名的新目录中,但扩展名为xcdatamodeld - 此目录中将有一个第二个文件,名称中包含2 。选择新文件,然后选择Design-> Data Model-> Set Current Version(
  4. 如果您已经进行了导致您的项目不兼容的更改,请从原始xcdatamodel文件中删除这些更改。如果您尚未进行更改,则只需编辑2.xcdatamodel文件(您刚刚创建的当前版本)。

  5. 现在,当您将此版本安装到具有


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



    我想我会做的是,我将保留所有更改的文件,然后一旦我准备好部署我的更新,我会删除所有的中间






    UPDATE(15/07/2011):



    感谢@ rockstarberlin 指出苹果更新的文档: / p>

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



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



    https:/ /developer.apple.com/library/ios/documentation/cocoa/Conceptual/CoreDataVersioning/Articles/vmModelFormat.html


    I have read in the documentation about automatic /lightweight migration for Core Data models - but am having problems in the reality of 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.

    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.

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

    Then you need to do a little trick in xCode:

    1. Select your xcdatamodel file
    2. Select the Design Menu at the top - then Data Model - then choose Add Model Version
    3. Your xcdatamodel file will then get moved into a new directory with the same name as your xcdatamodel file but with the extension xcdatamodeld - there will be a second file in this directory with a 2 in the name. Select the new file and then Design->Data Model->Set Current Version (in Xcode 4 you do this)
    4. If you have already made the changes that have caused your project to be incompatible - take these changes out of the original xcdatamodel file. If you have yet to make the changes - then just edit the 2.xcdatamodel file (the one you just made current version).
    5. Now when you install this version onto a device that has the old model - it will automatically upgrade that model to the new model.

    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.


    UPDATE (15/07/2011):

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

    Xcode 4: Setting a Managed Object Model’s Current Version

    Update: 8/19/2013 better link:

    https://developer.apple.com/library/ios/documentation/cocoa/Conceptual/CoreDataVersioning/Articles/vmModelFormat.html

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

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