iphone – cocoa错误134100再次 – (用于打开的模型与用于创建的模型不兼容…)

我在这里发布了这个问题,但总是,答案是模型已经改变,重置模拟器,删除商店.

我也收到了这个错误,但是,这是一个新的应用程序.我没有添加/更改实体或属性.

我删除了商店,我重置了模拟器,但我得到了相同的结果.

这是商店代码..这个问题还有其他原因吗?

- (NSManagedObjectModel *)managedObjectModel {
   //  NSLog(@"%s", __FUNCTION__);
    if (managedObjectModel_ != nil) {
        return managedObjectModel_;
    }

    NSString *modelPath = [[NSBundle mainBundle] pathForResource:@"Lexicon" ofType:@"momd"];
    NSURL *modelURL = [NSURL fileURLWithPath:modelPath];
    managedObjectModel_ = [[NSManagedObjectModel alloc] initWithContentsOfURL:modelURL];    
    return managedObjectModel_;
}

/**
 Returns the persistent store coordinator for the application.
 If the coordinator doesn't already exist, it is created and the application's store added to it.
 */
- (NSPersistentStoreCoordinator *)persistentStoreCoordinator {

    if (persistentStoreCoordinator_ != nil) {
        return persistentStoreCoordinator_;
    }

    NSURL *storeURL = [NSURL fileURLWithPath: [[self applicationDocumentsDirectory] stringByAppendingPathComponent: @"dict.sqlite"]];

    NSError *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_;
}

这是控制台喷出的:

2011-08-03 11:42:38.936 Lexicon[4468:f203] -[AppDelegate
application:didFinishLaunchingWithOptions:] 2011-08-03 11:42:38.989
Lexicon[4468:f203] -[AppDelegate applicationDocumentsDirectory]
2011-08-03 11:42:39.048 Lexicon[4468:f203] Unresolved error Error
Domain=NSCocoaErrorDomain Code=134100 “The operation couldn’t be
completed. (Cocoa error 134100.)” UserInfo=0x6d3ee60
{metadata={type = immutable dict,
count = 7, entries => 2 : {contents =
“NSStoreModelVersionIdentifiers”} = {type = immutable, count = 1, values = ( 0 : {length = 0, capacity = 0, bytes = 0x} )} 4 :
{contents =
“NSPersistenceFrameworkVersion”} = {value = +363, type = kCFNumberSInt64Type} 6 : {contents = “NSStoreModelVersionHashes”} =
{type = immutable dict, count = 1,
entries => 0 : {contents =
“LexiconEntity”} = {length = 32,
capacity = 32, bytes = 0x8698c5295fa5124b78a6b127bba26ff0 …
70eaece0517cd4c6} }

7 : {contents = “NSStoreUUID”} =
{contents =
“86B22D58-28A5-4585-8650-07111B34B43A”} 8 : {contents = “NSStoreType”} = {contents = “SQLite”} 9 : {contents = “_NSAutoVacuumLevel”} = {contents = “2”} 10 : {contents = “NSStoreModelVersionHashesVersion”} =
{value = +3, type =
kCFNumberSInt32Type} } , reason=The model used to open the store is
incompatible with the one used to create the store}, {
metadata = {
NSPersistenceFrameworkVersion = 363;
NSStoreModelVersionHashes = {
LexiconEntity = <8698c529 5fa5124b 78a6b127 bba26ff0
f3bc678b a4f1e809 70eaece0 517cd4c6>;
};
NSStoreModelVersionHashesVersion = 3;
NSStoreModelVersionIdentifiers = (
<>
);
NSStoreType = SQLite;
NSStoreUUID = “86B22D58-28A5-4585-8650-07111B34B43A”;
“_NSAutoVacuumLevel” = 2;
};
reason = “The model used to open the store is incompatible with
the one used to create the store”; }

最佳答案 事实证明,sqlite DB存储是错误构建的.因此,在这种情况下,消息相当准确.

上面的问题是真的.数据库没有反映模型.

解决方案是让Core Data从模型中创建空数据库,然后让Core Data自行导入数据.

我首先将我的sqlite数据库导出到sql,调用文件db.sql(imaginative!).我只导出数据表和主键表,而不是元数据表.我使用了一个名为SQLiteManager的应用程序.您也可以在命令行执行此操作.

除处理持久存储控制器外,所有代码都是库存.

该代码如下:

- (NSPersistentStoreCoordinator *)persistentStoreCoordinator {

    if (persistentStoreCoordinator_ != nil) {
        return persistentStoreCoordinator_;
    }


    NSString *storePath = [[self applicationDocumentsDirectory] stringByAppendingPathComponent:@"db.sql"];

    // set up the backing store
    NSFileManager *fileManager = [NSFileManager defaultManager];
    // If the expected store doesn't exist, copy the default store.
    if (![fileManager fileExistsAtPath:storePath]) {
        NSString *defaultStorePath = [[NSBundle mainBundle] pathForResource:@"dict" ofType:@"sqlite"];
        if (defaultStorePath) {
            [fileManager copyItemAtPath:defaultStorePath toPath:storePath error:NULL];
        }
    }

    NSURL *storeURL = [NSURL fileURLWithPath:storePath];

    NSError *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_;
}

我希望这有帮助..它似乎对我有用..

点赞