持久存储的核心数据错误

我正在尝试使用Core Data从模型加载简单数据并将其放入表视图中。 以下是我的持久存储的以下代码:

//AppDelegate.m - (NSPersistentStoreCoordinator *)persistentStoreCoordinator { if (__persistentStoreCoordinator != nil) { return __persistentStoreCoordinator; } NSURL *storeURL = [[self applicationDocumentsDirectory] URLByAppendingPathComponent:@"vofasmmmnmgd.sqlite"]; if (![[NSFileManager defaultManager] fileExistsAtPath:[storeURL path]]) { NSURL *preloadURL = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"CoreDataSQLite" ofType:@"sqlite"]]; NSError* err = nil; if (![[NSFileManager defaultManager] copyItemAtURL:preloadURL toURL:storeURL error:&err]) { NSLog(@"Oops, could copy preloaded data"); } } NSError *error = nil; __persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:[self managedObjectModel]]; if (![__persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeURL options:nil error:&error]) { /* Replace this implementation with code to handle the error appropriately. abort() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development. Typical reasons for an error here include: * The persistent store is not accessible; * The schema for the persistent store is incompatible with current managed object model. Check the error message to determine what the actual problem was. If the persistent store is not accessible, there is typically something wrong with the file path. Often, a file URL is pointing into the application's resources directory instead of a writeable directory. If you encounter schema incompatibility errors during development, you can reduce their frequency by: * Simply deleting the existing store: [[NSFileManager defaultManager] removeItemAtURL:storeURL error:nil] * Performing automatic lightweight migration by passing the following dictionary as the options parameter: [NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithBool:YES], NSMigratePersistentStoresAutomaticallyOption, [NSNumber numberWithBool:YES], NSInferMappingModelAutomaticallyOption, nil]; Lightweight migration will only work for a limited set of schema changes; consult "Core Data Model Versioning and Data Migration Programming Guide" for details. */ NSLog(@"Unresolved error %@, %@", error, [error userInfo]); abort(); } return __persistentStoreCoordinator; } 

如您所见,我正在从sqlite数据库中预加载数据。 一切都很完美。 它能够从模型中提取数据并正确显示。

当我将第二个实体添加到应用程序的最后部分的同一模型中时,情况发生了变化。 该应用程序决定崩溃“abort();” 说以下内容:

 "The model used to open the store is incompatible with the one used to create the store"; 

现在这是一个简单的解决方案,因为谷歌研究我发现了。 转到应用程序并删除它然后将重置所有数据库的数据并让它重新加载。 但是我已经这样做了,但仍然无法正常工作。 以下是我尝试过的所有结果仍然相同:

  • 删除应用
  • 重置iPhone模拟器上的内容和设置
  • 将storeURL的名称更改为其原始值以外的其他名称。
  • 删除上面代码注释中列出的商店URL
  • 清洁项目

我得出结论,这是因为我添加了另一个实体,因为当我删除它完全运行的实体并恢复正常而没有任何问题。

我不知道发生了什么令人沮丧,因为我不知道如何解决这个问题。 谷歌已经没有解决这个问题的解决方案,所以我想我会得到奇妙的Stack Overflow来获得一个奇迹般的解决方案来结束这种挫败感。 谢谢!

动机很简单。 修改模型时,Core Data不知道如何映射它。

直接来自Core Data doc

因此,更改模型将使其与先前创建的商店不兼容(因此无法打开)。 如果更改模型,则需要将现有存储中的数据更改为新版本 – 更改存储格式称为迁移。

如果为NSMigratePersistentStoresAutomaticallyOption设置YES ,对于NSInferMappingModelAutomaticallyOption ,则应该没问题。 这也称为LightWeight迁移。

 NSMutableDictionary *options = [NSMutableDictionary dictionary]; [options setValue:[NSNumber numberWithBool:YES] forKey:NSMigratePersistentStoresAutomaticallyOption]; [options setValue:[NSNumber numberWithBool:YES] forKey:NSInferMappingModelAutomaticallyOption]; NSPersistentStore *store = nil; store = [coordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeURL options:options error:&error]; 

编辑

只需将options字典传递给addPersistentStoreWithType:configuration:URL:options:error在以下代码段中创建的addPersistentStoreWithType:configuration:URL:options:error

 NSMutableDictionary *options = [NSMutableDictionary dictionary]; [options setValue:[NSNumber numberWithBool:YES] forKey:NSMigratePersistentStoresAutomaticallyOption]; [options setValue:[NSNumber numberWithBool:YES] forKey:NSInferMappingModelAutomaticallyOption]; _persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:[self managedObjectModel]]; if (![_persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeURL options:options error:&error]) { }