【问题标题】:Persistent store migration failed missing source managed object model持久存储迁移失败缺少源托管对象模型
【发布时间】:2011-03-30 20:37:31
【问题描述】:

我正在尝试扩展我的核心数据。所以我向我的实体添加了一个新属性并尝试使用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;
}  

【问题讨论】:

    标签: objective-c cocoa core-data


    【解决方案1】:

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

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

    【讨论】:

    • 我想,用户两个模型没有必要。所以你是对的,只有一个模型。也许第一个答案中推荐的书会给黑暗带来光明。
    • @lueda:我有它,它有。你需要两个模型。
    • Wohoo 现在可以工作了。正如您所说,我需要一个带有两个模型的版本化模型。这本书完成了其余的工作:)
    • 这里有一个问题:使用 Xcode4,您似乎无法轻松设置版本化模型并从 Finder 添加版本。拖放被拒绝。
    猜你喜欢
    • 1970-01-01
    • 2020-06-18
    • 2014-08-06
    • 1970-01-01
    • 1970-01-01
    • 2014-05-11
    • 2011-04-17
    • 1970-01-01
    • 2014-10-30
    相关资源
    最近更新 更多