我按照this指南在我的非基于文档的应用程序中实现核心数据迁移和版本控制。我做了报告驾驶,但当我开始应用程序告诉我,我打开的模型不支持。我该如何解决这个问题?
- (NSPersistentStoreCoordinator *)persistentStoreCoordinator
{
if (_persistentStoreCoordinator) {
return _persistentStoreCoordinator;
}
NSManagedObjectModel *mom = [self managedObjectModel];
if (!mom) {
NSLog(@"%@:%@ No model to generate a store from", [self class], NSStringFromSelector(_cmd));
return nil;
}
NSFileManager *fileManager = [NSFileManager defaultManager];
NSURL *applicationFilesDirectory = [self applicationFilesDirectory];
NSURL *storeURL = [[self applicationFilesDirectory] URLByAppendingPathComponent:@"Preventivi.storedata"];
NSError *error = nil;
//Turn on automatic store migration
NSMutableDictionary *optionsDictionary = [NSMutableDictionary dictionary];
[optionsDictionary setObject:[NSNumber numberWithBool:YES] forKey:NSMigratePersistentStoresAutomaticallyOption];
NSDictionary *properties = [applicationFilesDirectory resourceValuesForKeys:@[NSURLIsDirectoryKey] error:&error];
if (!properties) {
BOOL ok = NO;
if ([error code] == NSFileReadNoSuchFileError) {
ok = [fileManager createDirectoryAtPath:[applicationFilesDirectory path] withIntermediateDirectories:YES attributes:nil error:&error];
NSURL *defaultStoreURL = [[NSBundle mainBundle] URLForResource:@"Preventivi" withExtension:@"storedata"];
if (defaultStoreURL) {
[fileManager copyItemAtURL:defaultStoreURL toURL:storeURL error:NULL];
}
}
if (!ok) {
[[NSApplication sharedApplication] presentError:error];
return nil;
}
} else {
if (![properties[NSURLIsDirectoryKey] boolValue]) {
// Customize and localize this error.
NSString *failureDescription = [NSString stringWithFormat:@"Expected a folder to store application data, found a file (%@).", [applicationFilesDirectory path]];
NSMutableDictionary *dict = [NSMutableDictionary dictionary];
[dict setValue:failureDescription forKey:NSLocalizedDescriptionKey];
error = [NSError errorWithDomain:@"YOUR_ERROR_DOMAIN" code:101 userInfo:dict];
[[NSApplication sharedApplication] presentError:error];
return nil;
}
}
NSURL *url = [applicationFilesDirectory URLByAppendingPathComponent:@"Preventivi.storedata"];
NSPersistentStoreCoordinator *coordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:mom];
if (![coordinator addPersistentStoreWithType:NSXMLStoreType configuration:nil URL:url options:nil error:&error]) {
[[NSApplication sharedApplication] presentError:error];
return nil;
}
_persistentStoreCoordinator = coordinator;
return _persistentStoreCoordinator;
}
答案 0 :(得分:0)
你能说出应用程序抛出错误的位置吗?如果它在最后一行中,您可以尝试将storeType NSXMLStoreType更改为NSSQLiteStoreType,例如。
if (![coordinator addPersistentStoreWithType:NSXMLStoreType configuration:nil URL:url options:nil error:&error])
到
if (![coordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:url options:options error:&error])
为此,您还必须将后缀更改为“sqlite”。
NSURL *url = [NSURL fileURLWithPath: [applicationFilesDirectory stringByAppendingPathComponent: @"Preventivi.sqlite"]]
和
NSURL *defaultStoreURL = [[NSBundle mainBundle] URLForResource:@"Preventivi" withExtension:@"sqlite"];
或者如果你已经有了解决方案,你可以分享这个:) 但与我的代码相比,没有真正的差异(只有更多选项
)NSDictionary *options = [NSDictionary dictionaryWithObjectsAndKeys:
[NSNumber numberWithBool:YES], NSMigratePersistentStoresAutomaticallyOption,
[NSNumber numberWithBool:YES], NSInferMappingModelAutomaticallyOption, nil];
并且在添加持久存储时不使用选项^^ )
我希望这会有所帮助:)
答案 1 :(得分:0)
您似乎已将Core Data堆栈配置为使用自动迁移。并且在不使用版本控制的情况下继续更改模型。
您必须使用版本化模型。如果您在修改之前没有创建模型版本,则自动迁移将无法正常工作。为了在每次修改之前使自动迁移工作,您应该创建一个新的模型版本并修改该新版本。
例如,您想要将几个属性添加到实体并添加另一个实体,您:
如果您使用配置,您还应该说,只要自动迁移存在配置问题。