我正在尝试使用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数据库中预加载数据。一切都很完美。它能够从模型中提取数据并正确显示。
BUT
当我将第二个实体添加到应用程序的最后部分的同一模型中时,情况发生了变化。该应用程序决定崩溃“abort();”说以下内容:
"The model used to open the store is incompatible with the one used to create the store";
现在这是一个简单的解决方法,因为我已经发现因为谷歌的研究。转到应用程序并删除它然后将重置所有数据库的数据并让它重新加载。但是我已经这样做了,但仍然无法正常工作。以下是我尝试过的所有结果仍然相同:
我得出的结论是,因为我添加了另一个实体,因为当我删除它完全运行的实体时,它会恢复正常而没有任何问题。
我不知道发生了什么事非常令人沮丧,因为我不知道如何解决这个问题。谷歌已经没有解决这个问题的解决方案,所以我想我会得到奇妙的 Stack Overflow 来获得一个奇迹般的解决方案来结束这种挫败感。谢谢!
答案 0 :(得分:8)
动机很简单。修改模型时,Core Data不知道如何映射它。
直接来自Core Data doc
因此,更改模型会使其与之不相容(等等 无法打开它之前创建的商店。如果你改变你的 因此,您需要将现有商店中的数据更改为新数据 版本更改商店格式称为迁移。
如果您为YES
和NSMigratePersistentStoresAutomaticallyOption
设置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
,就像在以下代码段中创建一样。
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]) {
}