添加新的核心数据模型版本后出错

添加新的核心数据模型版本后出错

本文介绍了添加新的核心数据模型版本后出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我添加了一个新的模型版本,我将核心数据模型设置为使用新版本,但是当应用程序尝试启动时,我收到此错误。

I added a new model version, and I set the core data model to use that new version, but I get this error when the application tries to start.

用于打开持久存储的托管对象模型版本与用于创建持久存储的托管对象模型版本不兼容。

"The managed object model version used to open the persistent store is incompatible with the one that was used to create the persistent store."

>

我猜的问题是,当前持久存储是模型的旧版本。有没有办法只是删除它,所以它使一个新的?我不在乎保存任何数据。

I'm guessing the problem is that the current persistent store is the old version of the model. Is there a way to just delete it so it makes a new one? I don't care about saving any of that data.

推荐答案

您必须在不同版本之间进行迁移。根据苹果的文档,如果改变很简单,你可以做轻量级迁移。

You have to migrate between versions. According to Apple's docs, if the changes are simple, you can do lightweight migration.

将这些选项添加到NSPersistentStoreCoordinator似乎有效。

Adding these options to the NSPersistentStoreCoordinator seemed to work.

NSDictionary *options = [NSDictionary dictionaryWithObjectsAndKeys:
                                 [NSNumber numberWithBool:YES], NSMigratePersistentStoresAutomaticallyOption,
                                 [NSNumber numberWithBool:YES], NSInferMappingModelAutomaticallyOption, nil];

    NSURL *url = [applicationFilesDirectory URLByAppendingPathComponent:@"YOURAPP.storedata"];
        persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:mom];
        if (![persistentStoreCoordinator addPersistentStoreWithType:NSXMLStoreType configuration:nil URL:url options:options error:&error]) {
            [[NSApplication sharedApplication] presentError:error];
            [persistentStoreCoordinator release], persistentStoreCoordinator = nil;
            return nil;
        }

    return persistentStoreCoordinator;

这篇关于添加新的核心数据模型版本后出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-26 11:48