持久存储迁移失败,缺少源管理对象模型

时间:2011-03-30 20:37:31

标签: objective-c cocoa core-data

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

1 个答案:

答案 0 :(得分:3)

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

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