gpt4 book ai didi

objective-c - 持久存储迁移失败缺少源托管对象模型

转载 作者:搜寻专家 更新时间:2023-10-30 20:12:10 25 4
gpt4 key购买 nike

我正在尝试扩展我的核心数据。所以我向我的实体添加了一个新属性并尝试使用 Automatic Lightweight Migration .但是当我启动程序时,弹出错误 Persistent store migration failed missing source managed object model

谁知道哪里出了问题?

我的 AppDelegate.c 的相关部分(实际上我只添加了 NSDictionary *options):

- (NSPersistentStoreCoordinator *) persistentStoreCoordinator {


if (persistentStoreCoordinator) return persistentStoreCoordinator;

NSManagedObjectModel *mom = [self managedObjectModel];
if (!mom) {
NSAssert(NO, @"Managed object model is nil");
NSLog(@"%@:%@ No model to generate a store from", [self class], _cmd);
return nil;
}

NSFileManager *fileManager = [NSFileManager defaultManager];
NSString *applicationSupportDirectory = [self applicationSupportDirectory];
NSError *error = nil;

if ( ![fileManager fileExistsAtPath:applicationSupportDirectory isDirectory:NULL] ) {
if (![fileManager createDirectoryAtPath:applicationSupportDirectory withIntermediateDirectories:NO attributes:nil error:&error]) {
NSAssert(NO, ([NSString stringWithFormat:@"Failed to create App Support directory %@ : %@", applicationSupportDirectory,error]));
NSLog(@"Error creating application support directory at %@ : %@",applicationSupportDirectory,error);
return nil;
}
}

NSURL *url = [NSURL fileURLWithPath: [applicationSupportDirectory stringByAppendingPathComponent: @"stats.darx"]];
persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel: mom];
NSDictionary *options = [NSDictionary dictionaryWithObjectsAndKeys:
[NSNumber numberWithBool:YES], NSMigratePersistentStoresAutomaticallyOption,
[NSNumber numberWithBool:YES], NSInferMappingModelAutomaticallyOption, nil];
if (![persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType
configuration:nil
URL:url
options:options
error:&error]){
[[NSApplication sharedApplication] presentError:error];
[persistentStoreCoordinator release], persistentStoreCoordinator = nil;
return nil;
}

return persistentStoreCoordinator;
}

最佳答案

您需要使用版本化的托管对象模型,其中包含模型的两个版本。自动迁移仍然需要查看模型的现有版本和新版本,以便找出差异是什么以及如何处理它们。

您引用的错误表明您的应用程序包现在仅包含您的新模型(您要使用的模型),而不包含旧模型(您尝试从中迁移的模型)。返回到您的版本控制系统并检索旧模型,然后设置一个包含旧模型为 v1 和新模型为 v2 的版本化模型。

关于objective-c - 持久存储迁移失败缺少源托管对象模型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5492070/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com