【问题标题】:iOS application coreData with PersistentStore as static sqlite Data Migration?iOS 应用程序 coreData 与 PersistentStore 作为静态 sqlite 数据迁移?
【发布时间】:2015-07-07 05:35:25
【问题描述】:

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

【问题讨论】:

标签: ios objective-c sqlite core-data core-data-migration


【解决方案1】:

如果只是给实体添加属性,可以使用Apple documentation上的coredata轻量级迁移建议

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.
}

【讨论】:

    【解决方案2】:

    对于那些不想深入研究文档并正在寻找快速解决方案的人:

    1>打开您的 .xcdatamodeld 文件

    2> 点击编辑器

    3> 选择添加模型版本...

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

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

    6> 并在版本化核心数据模型下为当前数据模型选择新版本的数据模型

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

    8> 转到您的 AppDelegate 并找到创建 persistentStoreCoordinator 的位置

    9> 找到这一行 if (![_persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeURL options:nil error:&error])

    10> 将 nil 选项替换为 @{NSMigratePersistentStoresAutomaticallyOption:@YES, NSInferMappingModelAutomaticallyOption:@YES} (实际上在该方法的注释代码中提供)

    来吧,玩得开心! 附言这仅适用于轻量级迁移。为了使您的迁移符合轻量级迁移的条件,您的更改必须限制在这个窄范围内:

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

    将非可选属性设为可选。

    将可选属性设为非可选,只要您提供默认值即可。

    添加或删除实体。

    重命名属性

    重命名实体。

    【讨论】:

    • 非常感谢 Mr.Chetan kasundra 这个回答对我帮助很大
    猜你喜欢
    • 1970-01-01
    • 2018-03-19
    • 2013-04-09
    • 2016-07-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-06-08
    • 2013-08-03
    相关资源
    最近更新 更多