ios - 使用Core Data轻量级迁移和UIManagedDocument“找不到源存储的模型”

标签 ios objective-c core-data database-migration

我试图在现有的非常简单的Core Data模型的实体上简单地添加一个新属性...这是一场噩梦。

我创建了模型的新版本,并将新属性添加到新版本中。

model versioning

然后,我尝试运行(在iPhone 6上,因为您知道...模拟器)。
没有崩溃,但是出现了以下美丽的错误:

2015-08-13 15:27:47.963 MyApp[15605:6736100] CoreData: error: -addPersistentStoreWithType:SQLite configuration:(null) URL:file:///var/mobile/Containers/Data/Application/3FF51856-ECA5-4179-AA2A-FBE5EBFDDFD2/Documents/chat.db/StoreContent/persistentStore options:{
    NSInferMappingModelAutomaticallyOption = 1;
    NSMigratePersistentStoresAutomaticallyOption = 1;
    NSPersistentStoreRemoveStoreOnCleanupKey = 1;
} ... returned error Error Domain=NSCocoaErrorDomain Code=134130 "The operation couldn’t be completed. (Cocoa error 134130.)" UserInfo=0x176c61600 {URL=file:///var/mobile/Containers/Data/Application/3FF51856-ECA5-4179-AA2A-FBE5EBFDDFD2/Documents/chat.db/StoreContent/persistentStore, metadata={
    NSPersistenceFrameworkVersion = 519;
    NSStoreModelVersionHashes =     {
        Conversation = <e884e2fd 37f94dc9 7039539a cdebcb76 8c11637f 5f5e1744 310ce768 6bdf9158>;
        ConversationBinaryMessage = <dad3bd33 9cb61f03 f2895527 fa4a0b68 998bd8f3 d8196097 a0a8ccf0 e6076053>;
        ConversationLocationMessage = <6efd4294 b70e3110 cea51fd8 89471130 823cd964 0d7cdcd9 cdbc21e6 d995de0a>;
        ConversationMessage = <11bb9596 38fa5770 d5ace0c6 063da45e 3afa77fe d5d0f012 ba94dca3 2a61fb19>;
        ConversationStickerMessage = <be1d0d79 568c2ce5 ad9654d4 51dda203 9bc28162 8e50c9df 3d8b9d04 00d12294>;
        ConversationTextMessage = <7e79e7fd 32e5ef72 b98e544b 36073978 abf202ca fc78620b d200ca02 00a33181>;
        ...
    };
    NSStoreModelVersionHashesVersion = 3;
    NSStoreModelVersionIdentifiers =     (
        ""
    );
    NSStoreType = SQLite;
    NSStoreUUID = "BB890469-0382-4C74-8D6E-4708607395D8";
    "_NSAutoVacuumLevel" = 2;
}, reason=Can't find model for source store} with userInfo dictionary {
    URL = "file:///var/mobile/Containers/Data/Application/3FF51856-ECA5-4179-AA2A-FBE5EBFDDFD2/Documents/chat.db/StoreContent/persistentStore";
    metadata =     {
        NSPersistenceFrameworkVersion = 519;
        NSStoreModelVersionHashes =         {
            Conversation = <e884e2fd 37f94dc9 7039539a cdebcb76 8c11637f 5f5e1744 310ce768 6bdf9158>;
            ConversationBinaryMessage = <dad3bd33 9cb61f03 f2895527 fa4a0b68 998bd8f3 d8196097 a0a8ccf0 e6076053>;
            ConversationLocationMessage = <6efd4294 b70e3110 cea51fd8 89471130 823cd964 0d7cdcd9 cdbc21e6 d995de0a>;
            ConversationMessage = <11bb9596 38fa5770 d5ace0c6 063da45e 3afa77fe d5d0f012 ba94dca3 2a61fb19>;
            ConversationStickerMessage = <be1d0d79 568c2ce5 ad9654d4 51dda203 9bc28162 8e50c9df 3d8b9d04 00d12294>;
            ConversationTextMessage = <7e79e7fd 32e5ef72 b98e544b 36073978 abf202ca fc78620b d200ca02 00a33181>;
            ...
        };
        NSStoreModelVersionHashesVersion = 3;
        NSStoreModelVersionIdentifiers =         (
            ""
        );
        NSStoreType = SQLite;
        NSStoreUUID = "BB890469-0382-4C74-8D6E-2015-08-13 15:27:47.978 MyApp[15605:6736100] CoreData: annotation: NSPersistentStoreCoordinator's current model hashes are {
    Conversation = <18f45c07 25844dcb 9c392f93 5b108dd5 1aa8bcb7 ac596cda 65d09e4e e1e1e85c>;
    ConversationBinaryMessage = <dad3bd33 9cb61f03 f2895527 fa4a0b68 998bd8f3 d8196097 a0a8ccf0 e6076053>;
    ConversationLocationMessage = <6efd4294 b70e3110 cea51fd8 89471130 823cd964 0d7cdcd9 cdbc21e6 d995de0a>;
    ConversationMessage = <11bb9596 38fa5770 d5ace0c6 063da45e 3afa77fe d5d0f012 ba94dca3 2a61fb19>;
    ConversationStickerMessage = <be1d0d79 568c2ce5 ad9654d4 51dda203 9bc28162 8e50c9df 3d8b9d04 00d12294>;
    ConversationTextMessage = <7e79e7fd 32e5ef72 b98e544b 36073978 abf202ca fc78620b d200ca02 00a33181>;
}

所以Conversation = <e884e2fd 37f94dc9 7039539a cdebcb76 8c11637f 5f5e1744 310ce768 6bdf9158>;
成为Conversation = <18f45c07 25844dcb 9c392f93 5b108dd5 1aa8bcb7 ac596cda 65d09e4e e1e1e85c>;
这是意料之中的。

这是我初始化文档的方式:
+ (UIManagedDocument *)sharedManagedDocumentForChat {
    static UIManagedDocument *managedDocument;
    static dispatch_once_t onceToken;

    dispatch_once(&onceToken, ^{
        NSFileManager *fileManager         = [NSFileManager defaultManager];
        NSArray       *documentDirectories = [fileManager URLsForDirectory:NSDocumentDirectory
                                                                 inDomains:NSUserDomainMask];

        if (documentDirectories.count) {
            NSURL *url = [documentDirectories[0] URLByAppendingPathComponent:@"chat.db"];
            managedDocument = [[UIManagedDocument alloc] initWithFileURL:url];
            managedDocument.persistentStoreOptions = @{ NSMigratePersistentStoresAutomaticallyOption:   @YES,
                                                        NSInferMappingModelAutomaticallyOption:         @YES };
        }
    });

    return managedDocument;
}

  • 我在这里想念什么?
  • 为什么核心数据找不到源存储的模型?

  • 感谢您的任何提示。

    附注:我有2个不同的Core Data数据库,其中1个用于聊天功能,另外1个用于缓存服务器响应,这可能会引起问题吗?

    编辑

    如果有帮助,这里有两个模型。
    models

    最佳答案

    您的项目中是否还有原始(版本1)模型文件?

    每次修订模型文件时,都需要制作一个副本。

    苹果文件

    要执行自动迁移,Core Data必须能够在运行时查找源和目标管理对象模型本身(请参阅Core Data必须能够推断映射)。如果需要将模型放置在自动发现未检查的位置,则需要生成推断的模型并使用迁移管理器(NSMigrationManager的实例)自己启动迁移。

    关于ios - 使用Core Data轻量级迁移和UIManagedDocument“找不到源存储的模型”,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31990764/

    相关文章:

    ios - 核心数据 - "days"和 "tasks"(对多关系) - 如何获取一天的所有任务?

    ios - XE8.1 : file was built for archive which is not the architecture being linked (arm64)

    ios - 移动多个 UIImageView 对象

    ios - 分页UIScrollView如何添加多个UIImageView?

    ios - 如何在 UITableViewCell 上添加按钮

    iOS - CoreData -> 删除不再有关系的对象

    core-data - 使用带有根 UITabBarController 的 Storyboard传递 ManagedObjectContext 以查看 Controller

    ios - 如何修复 : Cannot assign value of type 'CLLocationDegrees' (aka 'Double' ) to type 'Float!' in Swift 3

    ios - 如何为带有部分的表格 View 单元格设置备用背景颜色

    ios - 在iOS中的 slider 值上旋转图像