添加新的核心数据模型版本后出错

时间:2011-04-06 21:06:49

标签: objective-c macos core-data

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

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

enter image description here

我猜测问题是当前的持久存储是该模型的旧版本。有没有办法只删除它,所以它是一个新的?我不关心保存任何数据。

3 个答案:

答案 0 :(得分: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;

答案 1 :(得分:4)

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

只需更改App Delegate中的persistentStoreCoordinator getter,如下所示:

- (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;
}

答案 2 :(得分: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