iOS在向现有核心数据添加更多关系之后如何利用核心数据? [英] IOS How to make use of core data after adding some more relationship to existing core data?

查看:48
本文介绍了iOS在向现有核心数据添加更多关系之后如何利用核心数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我决定通过添加少量关系(2-3)关系来更改我的核心数据结构。如何切换到新的核心数据堆栈,但仍保留旧的实现(方法,数据...)。我们是否需要创建新的 xcdatamodel 以及如何切换到新的xcdatamodel?

I've decided to change my core data structure by adding few relationship (2-3) relationship. How to switch to the new core data stack but still keeping the old implementation (method, data....). Do we need to create new xcdatamodel and how switch to new xcdatamodel?

任何帮助都是值得的

推荐答案

更改数据模型时,还需要将更改也通知给CodeData。为此,需要 lightWeightMigration。
为了告诉Core Data我们要执行轻量级迁移,在startCoreData()方法中配置持久性存储协调器时,我们需要设置两个属性。
首先,NSMigratePersistentStoresAutomaticallyOption必须为true,它告诉Core Data在模型更改时升级其SQLite数据库。
其次,NSInferMappingModelAutomaticallyOption也必须为true,它告诉Core Data找出模型更改时的差异,并在可能的情况下应用合理的默认值
创建optionsDictionary,如下所示:
let mOptions = [NSMigratePersistentStoresAutomaticallyOption:true,
NSInferMappingModelAutomaticallyOption:true]
将此选项传递给coordinator.addPersistentStoreWithType(NSSQLiteStoreType,配置:nil,URL:url,选项:mOptions)中的字典
而且,您准备好了!

When you change your data model, you need to inform CodeData of the change as well. For this a'lightWeightMigration' is needed. To tell Core Data we want to perform a lightweight migration, we need to set two properties when configuring our persistent store coordinator in the startCoreData() method. First, NSMigratePersistentStoresAutomaticallyOption needs to be true, which tells Core Data to upgrade its SQLite database when the model changes. Second, NSInferMappingModelAutomaticallyOption also needs to be true, which tells Core Data to figure out the differences when the model changes, and apply sensible defaults if possible create the optionsDictionary as this : let mOptions = [NSMigratePersistentStoresAutomaticallyOption: true, NSInferMappingModelAutomaticallyOption: true] pass this optionsDictionary in coordinator.addPersistentStoreWithType(NSSQLiteStoreType, configuration: nil, URL: url, options: mOptions) And you are good to go!

这篇关于iOS在向现有核心数据添加更多关系之后如何利用核心数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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