Core Data:falied to create persistentStoreCoordinator

错误描述

使用Core Data时碰到以下错误:

falied to create persistentStoreCoordinator The operation couldn’t be completed. (Cocoa error 134100.)

具体描述为:

reason = "The model used to open the store is incompatible with the one used to create the store"

产生原因

在数据库生成好之后,修改了其中一个属性的Optional选项。


optional选项.png

修改该选项后,数据库发生改变,需要迁移。

解决办法

创建存储器NSPersistentStoreCoordinator时,开启两个选项:

  • NSMigratePersistentStoresAutomaticallyOption
  • NSInferMappingModelAutomaticallyOption

代码如下:

NSDictionary *options = @{NSMigratePersistentStoresAutomaticallyOption: @(YES),
                          NSInferMappingModelAutomaticallyOption: @(YES)};
// 为 persistentStoreCoordinator 指定本地存储的类型,这里指定的是 SQLite
[_persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType
                                          configuration:nil
                                                    URL:sqliteURL
                                                options:options
                                                  error:&error];

这样就解决了。

可能引发此错误的操作:

  • 修改属性
  • 修改实体
  • 修改关联
  • 修改属性的optional选项

如果改动较大,还需要重新创建model才能解决问题。
详见:https://stackoverflow.com/questions/8881453/the-model-used-to-open-the-store-is-incompatible-with-the-one-used-to-create-the

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • rljs by sennchi Timeline of History Part One The Cognitiv...
    sennchi阅读 7,452评论 0 10
  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 135,010评论 19 139
  • 一叶孤舟对岸来, 三个甲子两鬓白, 半部春秋读不透, 红尘往事无处载。 英雄难过铜雀台, 得意褪去尽是哀, 风云际...
    郑三易阅读 225评论 0 0
  • 八卦的开头一般是,你俩怎么认识的?谁先追谁的? 我们是在大学的QQ群里认识的,高三的暑假已经确定大学的归属,希望早...
    无尾熊自成长阅读 234评论 0 1
  • 晚上接到家里的电话,叫我辞职回家,说帮我找好了工作,差点以为今天一夜肯定睡不着了,还好哥哥说再帮我看,还没确定下来...
    Spring柴柴阅读 238评论 3 0