2011-04-06 56 views
2

我添加了新的模型版本,并将核心数据模型设置为使用该新版本,但是当应用程序尝试启动时出现此错误。添加新的核心数据模型版本后出现错误

“用于打开持久存储的托管对象模型版本与用于创建持久存储的版本不兼容。”

enter image description here

我猜问题是,当前的持久性存储是旧版本的模型。有没有一种方法可以删除它,使它成为一个新的?我不在乎保存任何数据。

回答

7

您必须在版本之间迁移。根据Apple的文档,如果更改很简单,则可以进行轻量级迁移。

http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/CoreDataVersioning/Articles/vmLightweight.html#//apple_ref/doc/uid/TP40008426-SW1

添加这些选项到NSPersistentStoreCoordinator似乎工作。

NSDictionary *options = [NSDictionary dictionaryWithObjectsAndKeys: 
           [NSNumber numberWithBool:YES], NSMigratePersistentStoresAutomaticallyOption, 
           [NSNumber numberWithBool:YES], NSInferMappingModelAutomaticallyOption, nil]; 

    NSURL *url = [applicationFilesDirectory URLByAppendingPathComponent:@"YOURAPP.storedata"]; 
     persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:mom]; 
     if (![persistentStoreCoordinator addPersistentStoreWithType:NSXMLStoreType configuration:nil URL:url options:options error:&error]) { 
      [[NSApplication sharedApplication] presentError:error]; 
      [persistentStoreCoordinator release], persistentStoreCoordinator = nil; 
      return nil; 
     } 

    return persistentStoreCoordinator; 
0

找出您的应用存储文档的位置并将其放入垃圾桶。

但作为一个扩展的评论,你不妨检查各地NSPersistentStoreCoordinator显性和隐性迁移的可能性和选项。

- (NSPersistentStore *)addPersistentStoreWithType:(NSString *)storeType configuration:(NSString *)configuration URL:(NSURL *)storeURL options:(NSDictionary *)options error:(NSError **)error

根据不同的版本是多么的不同,你可以得到它的发生自动地通过使NSMigratePersistentStoresAutomaticallyOption & NSInferMappingModelAutomaticallyOption

也Theres

- (NSPersistentStore *)migratePersistentStore:(NSPersistentStore *)store toURL:(NSURL *)URL options:(NSDictionary *)options withType:(NSString *)storeType error:(NSError **)error

4

在回答你的问题时,“有没有办法删除它,以便它只是一个新的?”

是的。

只要改变persistentStoreCoordinator吸气剂应用程式委派如下:

- (NSPersistentStoreCoordinator *) persistentStoreCoordinator { 
    if (persistentStoreCoordinator) return persistentStoreCoordinator; 
    NSManagedObjectModel *mom = [self managedObjectModel]; 
    if (!mom) { 
    NSAssert(NO, @"Managed object model is nil"); 
    NSLog(@"%@:%s No model to generate a store from", [self class], (char *)_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: @"storedata"]]; 
    persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel: mom]; 
    if (![persistentStoreCoordinator addPersistentStoreWithType:NSXMLStoreType 
               configuration:nil 
                  URL:url 
                 options:nil 
                 error:&error]){ 
    // EDIT: if error opening persistent store, remove it and create a new one 
    if([[error domain] isEqualToString:@"NSCocoaErrorDomain"] && [error code] == 134100) { 
     NSLog(@"Core Data model was updated. Deleting old persistent store."); 
     [[NSFileManager defaultManager] removeItemAtURL:url error:nil]; 
     if (![persistentStoreCoordinator addPersistentStoreWithType:NSXMLStoreType 
               configuration:nil 
                  URL:url 
                 options:nil 
                 error:&error]){ 
     [[NSApplication sharedApplication] presentError:error]; 
     [persistentStoreCoordinator release], persistentStoreCoordinator = nil; 
     return nil; 
     } 
    } else { 
     [[NSApplication sharedApplication] presentError:error]; 
     [persistentStoreCoordinator release], persistentStoreCoordinator = nil; 
     return nil; 
    } 
    // 
    }  
    return persistentStoreCoordinator; 
}