具有PersistentStore的iOS应用程序coreData作为静态sqlite数据迁移?

该应用程序正在使用捆绑目录中的静态sqlite(初始数据)作为Coredata的持久性存储。 sqlite有7个表,其中一个表是通过添加额外的列/字段来修改的。 我如何让coreData了解持久性存储(store)已更改,并且需要进行新的更新? 有没有像我们为coredata做任何模型版本的概念?

对于那些不想深入查看文档并正在寻求快速修复的人:

1>打开你的.xcdatamodeld文件

2>点击编辑器

3>select添加模型版本…

4>添加模型的新版本(添加新的数据模型组)

5>select主文件,打开文件检查器(右侧面板)

6>和Versioned核心数据模型下,为当前数据模型select新版本的数据模型

7>这不是全部)你应该执行所谓的“光线迁移”。

8>转到您的AppDelegate并find正在创buildpersistentStoreCoordinator的位置

9>查找此行if(![persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeURL options:nil error:&error])

10>用@ {NSMigratePersistentStoresAutomaticallyOption:@YES,NSInferMappingModelAutomaticallyOption:@YES}replace零选项(实际上在该方法的注释代码中提供)

在这里,你去,玩得开心! PS这仅适用于轻量级迁移。 为了使您的迁移符合轻量级迁移的要求,您的更改必须仅限于此窄带:

添加或删除一个属性(属性或关系)。

使非可选属性可选。

只要您提供一个默认值,就可以select非自选属性。

添加或删除一个实体。

重命名一个属性

重命名一个实体。

如果您只是将属性添加到实体中,则可以使用Apple文档中的coredata轻量级迁移build议

NSError *error = nil; NSURL *storeURL = <#The URL of a persistent store#>; NSPersistentStoreCoordinator *psc = <#The coordinator#>; NSDictionary *options = [NSDictionary dictionaryWithObjectsAndKeys: [NSNumber numberWithBool:YES], NSMigratePersistentStoresAutomaticallyOption, [NSNumber numberWithBool:YES], NSInferMappingModelAutomaticallyOption, nil]; BOOL success = [psc addPersistentStoreWithType:<#Store type#> configuration:<#Configuration or nil#> URL:storeURL options:options error:&error]; if (!success) { // Handle the error. }