1

I did some changes at my CoreData Model. So far, I added a attribute called 'language'. When my application is launched and I click on "Create new Customer" a instance variable Customer is created. This variable is created by:

Customer *newCustomer = [NSEntityDescription insertNewObjectForEntityForName:@"Customer" inManagedObjectContext:appDelegate.managedObjectContext];

Before I did these changes everything worked fine and as planned. But now i get a dump with this error message:reason = "The model used to open the store is incompatible with the one used to create the store";

What do I have to do to solve this? reseting the persistence store didn't help so far.

4

2 回答 2

2

我为解决这个问题所做的就是添加这个

[[NSFileManager defaultManager] removeItemAtURL:storeURL error:nil];

在添加持久存储之前,在持久存储协调器中添加到我的 appDelegate。这将删除不再与您的数据模型兼容的现有存储。如果您想保留存储的内容,请记住在下次运行应用程序之前注释此行。

当我必须删除旧商店时,我的 persistentStoreCoordinator 实现看起来像这样。

- (NSPersistentStoreCoordinator *)persistentStoreCoordinator {

if (persistentStoreCoordinator_ != nil) {
    return persistentStoreCoordinator_;
}
NSError *error = nil;
NSURL *storeURL = [NSURL fileURLWithPath: [[self applicationDocumentsDirectory] stringByAppendingPathComponent: @"MyPinballScore.sqlite"]];
//The following line removes your current store so that you can create a new one that is compatible with your new model
[[NSFileManager defaultManager] removeItemAtURL:storeURL error:nil];

persistentStoreCoordinator_ = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:[self managedObjectModel]];
if (![persistentStoreCoordinator_ addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeURL options:nil error:&error]) {

    NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
    abort();
}    

return persistentStoreCoordinator_;

}

于 2010-10-06T13:35:22.887 回答
0

答案有点棘手,但这总是对我有用。这是为了全新安装新的兼容 .sqlite 文件,不是迁移

启动模拟器,删除应用程序和数据(删除应用程序后的弹出窗口)。

退出模拟器

在对数据模型进行任何编辑后打开 X-Code

删除{*appname*}.sqlite文件(或备份,从项目文件夹中删除,并删除参考)

清理应用程序 ( Product > Clean)

在模拟器中运行应用程序(对于本教程,我将假设 4.2)

在模拟器运行时,在 Finder 窗口中,导航至: {*home*} > Library > Application Support > iPhone Simulator > 4.2 > Applications > {*random identifier*} > Documents > {*appname*}.sqlite

将此文件复制到另一个位置

停止在 X-Code 中运行您的应用程序

将 { appname }.sqlite 文件拖放到 X-Code 中的文件列表中。

在弹出的对话框中,确保copy to folder选中复选框。

Product > Clean

然后再次在模拟器中运行应用程序

现在你应该有一个可以工作的 sqlite 文件了!

干杯,罗伯特

于 2011-11-23T02:03:05.787 回答