iOS应用程序coreData与PersistentStore作为静态sqlite数据迁移? [英] iOS application coreData with PersistentStore as static sqlite Data Migration?
问题描述
应用程序使用来自bundle目录的静态sqlite(初始数据)作为Coredata的持久存储。 sqlite有7个表,其中一个表通过添加额外的列/字段来修改。我如何让coreData了解持久存储(存储)已更改,并需要采取新的更新?
像我们为coredata做的那样,是否有任何模型版本的概念?
1>打开您的.xcdatamodeld文件
2>点击编辑器
3>选择添加模型版本...
4>添加您的模型的新版本(添加的新组数据模型)
5>选择主文件,打开文件检查器(右侧面板) p>
6>并在版本核心数据模型下为当前数据模型选择新版本的数据模型
7>
8>转到您的AppDelegate,找到persistStoreCoordinator的创建位置
9>查找此行if(![_ persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeURL options:nil error:& error])
10>使用@ {NSMigratePersistentStoresAutomaticallyOption:@YES,NSInferMappingModelAutomaticallyOption:@YES}替换nil选项(实际上在该方法的注释代码中提供)
乐趣!
P.S.这只适用于轻量级迁移。要使迁移成为轻量级迁移,您的更改必须局限于此窄带:
添加或删除属性(属性或关系)。
Make a nonoptional property optional。
重命名属性。
p> The app is using a static sqlite(initial data) from bundle directory as a persistent store for Coredata. The sqlite has 7 tables of which one table is modified by adding an extra column/field. How does I make coreData understand that the persistent store(store) is changed and it needs to take the new update ?
Is there any Model version concept for sqlite like we do for coredata ? For those who doesn't want to dig into documentation and is searching for a quick fix: 1>Open your .xcdatamodeld file 2> click on Editor 3> select Add model version... 4> Add a new version of your model (the new group of datamodels added) 5> select the main file, open file inspector (right-hand panel) 6> and under Versioned core data model select your new version of data model for current data model 7> THAT'S NOT ALL ) You should perform so called "light migration". 8> Go to your AppDelegate and find where the persistentStoreCoordinator is being created 9> Find this line if (![_persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeURL options:nil error:&error]) 10> Replace nil options with @{NSMigratePersistentStoresAutomaticallyOption:@YES, NSInferMappingModelAutomaticallyOption:@YES} (actually provided in the commented code in that method) Here you go, have fun!
P.S. This only applies for lightweight migration. For your migration to qualify as a lightweight migration, your changes must be confined to this narrow band: Add or remove a property (attribute or relationship). Make a nonoptional property optional. Make an optional attribute nonoptional, as long as you provide a default value. Add or remove an entity. Rename a property. Rename an entity. 这篇关于iOS应用程序coreData与PersistentStore作为静态sqlite数据迁移?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!