CoreData:不能轻量级迁移删除实体“没有这样的表”

Posted

技术标签:

【中文标题】CoreData:不能轻量级迁移删除实体“没有这样的表”【英文标题】:CoreData: cannot lightweight migrate deletion of entity 'no such table' 【发布时间】:2014-02-17 11:17:21 【问题描述】:

我正在尝试通过轻量级迁移来迁移两个 Core Data 模型。不同之处仅在于,我在新模型中删除了一堆实体,因为它们不再需要了。

运行我的应用程序时,在启动时,我收到以下错误消息:

CoreData: error: (1) I/O error for database at /var/mobile/Applications/E117D13D-C4DE-4F70-BBDB-F3F4E36A6A86/Documents/.Database.sqlite.migrationdestination_41b5a6b5c6e848c462a8480cd24caef3.  SQLite error code:1, 'no such table: ZFLIGHTSTATUSSEARCH'

FlightStatusSearch 是我删除的实体之一。我很确定我已经删除了几次实体,轻量级迁移就像一个魅力。我不知道我在这里会做错什么,因为它与我在这个应用程序中使用了几年的核心数据实现相同,没有任何问题。我在 ios7 上运行。

在启动过程中会触发几次错误消息。据我所知,每次我尝试与NSPersistentStoreCoordinator 交互时都会发生这种情况。

有没有人遇到过类似的问题或知道这个错误试图告诉我什么?

更新:

调用-[NSPersistentStoreCoordinator addPersistentStoreWithType:URL:options:error:]时出现此错误日志中的第一个

商店类型是SQLite,配置是Main,这是我在应用周围使用的主要配置的名称,URL指的是旧的,尚未迁移的数据库,选项是这样的:

NSDictionary* optionsDictionary = @NSMigratePersistentStoresAutomaticallyOption: @YES,
                                    NSInferMappingModelAutomaticallyOption: @YES;

实例化协调器时使用的模型包含FlightStatusSearch模型,错误信息指的是。

更新 2

这是堆栈跟踪:

#0  0x389c16a0 in objc_exception_throw ()
#1  0x2e4178aa in -[NSSQLiteConnection prepareSQLStatement:] ()
#2  0x2e4d0c08 in -[NSSQLConnection prepareAndExecuteSQLStatement:] ()
#3  0x2e50c66e in -[_NSSQLiteStoreMigrator performMigration:] ()
#4  0x2e505fa4 in -[NSSQLiteInPlaceMigrationManager migrateStoreFromURL:type:options:withMappingModel:toDestinationURL:destinationType:destinationOptions:error:] ()
#5  0x2e4bcb96 in -[NSMigrationManager migrateStoreFromURL:type:options:withMappingModel:toDestinationURL:destinationType:destinationOptions:error:] ()
#6  0x2e4fe3f0 in -[NSStoreMigrationPolicy(InternalMethods) migrateStoreAtURL:toURL:storeType:options:withManager:error:] ()
#7  0x2e4fd6f8 in -[NSStoreMigrationPolicy migrateStoreAtURL:withManager:metadata:options:error:] ()
#8  0x2e4fec5e in -[NSStoreMigrationPolicy(InternalMethods) _gatherDataAndPerformMigration:] ()
#9  0x2e40b0ba in -[NSPersistentStoreCoordinator addPersistentStoreWithType:configuration:URL:options:error:] ()
#10 0x001dca70 in -[CoreDataManager persistentStoreCoordinator] at /Users/michael/Projects/12_IP_MyProject/Components/CoreData/CoreDataManager.m:160
#11 0x000afed0 in -[AppDelegate init] at /Users/michael/Projects/12_IP_MyProject/MyProject/Classes/AppDelegate.m:86
#12 0x30eb2190 in UIApplicationMain ()
#13 0x000af9d4 in main at /Users/michael/Projects/12_IP_MyProject/MyProject/Supporting Files/main.m:16
#14 0x38ebaab6 in start ()

最好的问候, 迈克尔

【问题讨论】:

数据库最初是由 Core Data 创建的吗? 有没有可能是迁移完成了,但是你却试图用旧模型访问新的商店?在调用哪些方法时究竟会发生错误? 感谢您的反馈,我已经更新了我的问题。 @eofster 是的,这个数据库是 CoreData 在应用程序首次启动时创建的。 【参考方案1】:

我想通了。

我忘记删除一个具有以下环境的实体:

A 类继承自 B 类,A 类代表我忘记删除的实体,B 类一直是 Core Data 实体但被转换为非 coredata 模型。现在有一个从非核心数据模型继承的实体。这会导致我上面提到的错误消息。那里提到的表是 B 类的表,它曾经是一个实体,但现在不再是了。当我从模型中删除 A 类时,一切正常。

感谢您的帮助。

【讨论】:

以上是关于CoreData:不能轻量级迁移删除实体“没有这样的表”的主要内容,如果未能解决你的问题,请参考以下文章

CoreData 轻量级迁移 + 自定义

CoreData 迁移和数据映射:从现有属性创建新实体

Core Data 轻量级迁移 - 现有实体是不是添加了新属性?

从实体中删除属性后迁移 CoreData 时出错

在 CoreData 实体中将属性从 NSNumber 转换为 NSString - LightWeightMigration

Coredata 轻量级迁移失败