xcode 此 NSPersistentStoreCoordinator 没有持久存储(架构不匹配或迁移失败)。它无法执行保存操作
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/36130416/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
This NSPersistentStoreCoordinator has no persistent stores (schema mismatch or migration failure). It cannot perform a save operation
提问by Uzair Dhada
I am working on a application in which we are using x.x.xcdatamodel. Now in same x.x.xcdatamodel I have added an attribute in one of the entity. The application crashes showing the message "This NSPersistentStoreCoordinator has no persistent stores (schema mismatch or migration failure). It cannot perform a save operation.". I tried many things and i am also using lightweight migration to handle the situation but that is not working as well.Below is my code:
我正在开发一个使用 xxxcdatamodel 的应用程序。现在在同一个 xxxcdatamodel 中,我在实体之一中添加了一个属性。应用程序崩溃并显示消息“此 NSPersistentStoreCoordinator 没有持久存储(架构不匹配或迁移失败)。它无法执行保存操作。”。我尝试了很多事情,我也在使用轻量级迁移来处理这种情况,但这并不奏效。下面是我的代码:
- (NSPersistentStoreCoordinator *)persistentStoreCoordinator
{
if (__persistentStoreCoordinator != nil)
{
return __persistentStoreCoordinator;
}
NSURL *storeURL = [[self applicationDocumentsDirectory] URLByAppendingPathComponent:@"myApp.sqlite"];
NSError *error = nil;
__persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:[self managedObjectModel]];
NSDictionary *options = @{
NSMigratePersistentStoresAutomaticallyOption : @YES,
NSInferMappingModelAutomaticallyOption : @YES,
NSSQLitePragmasOption : @{@"journal_mode" : @"DELETE"}
};
if(![__persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeURL options:options error:&error])
{
NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
}
return __persistentStoreCoordinator;
}
- (BOOL) saveContext
{
@synchronized (_localStorage) {
//NSLog(@"----------------------------Save context called---------------------------");
BOOL result = TRUE;
NSError *error = nil;
NSManagedObjectContext *managedObjectContext = self.managedObjectContext;
if (managedObjectContext != nil)
{
//Crashes here at this line.
if ([managedObjectContext hasChanges] && ![managedObjectContext save:&error])
{
NSLog(@"----------------------------Save context failed---------------------------");
result = FALSE;
NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
}
}
//NSLog(@"----------------------------Save context completed---------------------------");
return result;
}
}
Am i missing something over here? OR Is it like i have to perform complete migration even if i add a single attribute in an entity?Thanks in advance.
我在这里错过了什么吗?或者,即使我在实体中添加单个属性,我是否也必须执行完整迁移?提前致谢。
回答by Tom Harrington
You don't have to do the migration yourself here. You dohave to add a new version of the data model. You can't edit the xcdatamodel
and expect Core Data to just use the new version. You need to keep your existing model, create a new version, and make your changes in the new version. You must always have a version of the model that matches the persistent store file.
您不必在这里自己进行迁移。你也必须添加数据模型的新版本。您不能编辑xcdatamodel
并期望 Core Data 只使用新版本。您需要保留现有模型、创建新版本并在新版本中进行更改。您必须始终拥有与持久存储文件匹配的模型版本。
You create a new version by selecting the xcdatamodel
model file in Xcode's file browser, going to the "Editor" menu, and selecting "Add Model Version..."
您可以通过xcdatamodel
在 Xcode 的文件浏览器中选择模型文件,转到“编辑器”菜单,然后选择“添加模型版本...”来创建新版本。
回答by Ali Baqbani
I work on a project and faced a similar problem, it seems that the former developer forgot to pass these two options for a lightweight migration. I passed in the second one and the migration completed successfully.
我在做一个项目,遇到了类似的问题,之前的开发者好像忘记通过这两个选项进行轻量级迁移了。我通过了第二个,迁移成功完成。
You request automatic lightweight migration using the options dictionary you pass in
addPersistentStoreWithType:configuration:URL:options:error:
, by setting values corresponding to both theNSMigratePersistentStoresAutomaticallyOption
and theNSInferMappingModelAutomaticallyOption
keys toYES
:
您可以使用传入的选项字典请求自动轻量级迁移
addPersistentStoreWithType:configuration:URL:options:error:
,方法是将与NSMigratePersistentStoresAutomaticallyOption
和NSInferMappingModelAutomaticallyOption
键对应的值设置为YES
:
NSError *error = nil;
NSURL *storeURL = [[NSBundle mainBundle] URLForResource:@"Model" withExtension:@"momd"];
NSPersistentStoreCoordinator *psc = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:managedObjectModel]
NSDictionary *options = [NSDictionary dictionaryWithObjectsAndKeys: [NSNumber numberWithBool:YES], NSMigratePersistentStoresAutomaticallyOption, [NSNumber numberWithBool:YES], NSInferMappingModelAutomaticallyOption, nil];
BOOL success = [psc addPersistentStoreWithType:NSSQLiteStoreType
configuration:nil URL:storeURL
options:options error:&error];
if (!success) {
// Handle the error.
}