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

发布于 2024-10-31 00:39:28 字数 232 浏览 1 评论 0原文

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

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

在此处输入图像描述

我猜测问题在于当前的持久存储是模型的旧版本。有没有办法直接删除它,然后再创建一个新的?我不关心保存任何数据。

I added a new model version, and I set the core data model to use that new version, but I get this error when the application tries to start.

"The managed object model version used to open the persistent store is incompatible with the one that was used to create the persistent store."

enter image description here

I'm guessing the problem is that the current persistent store is the old version of the model. Is there a way to just delete it so it makes a new one? I don't care about saving any of that data.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

ヅ她的身影、若隐若现 2024-11-07 00:39:28

您必须在版本之间迁移。根据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;

You have to migrate between versions. According to Apple's docs, if the changes are simple, you can do lightweight migration.

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

Adding these options to the NSPersistentStoreCoordinator seemed to work.

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;
独行侠 2024-11-07 00:39:28

回答你的问题,“有没有办法删除它,这样它就可以创建一个新的?”

是的。

只需更改 App Delegate 中的 permanentStoreCoordinator 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;
}

In answer to your question, "Is there a way to delete it so it just makes a new one ?"

Yes.

Just change the persistentStoreCoordinator getter in your App Delegate as follows:

- (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;
}
旧梦荧光笔 2024-11-07 00:39:28

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

但作为扩展评论,您可能希望检查 NSPersistentStoreCoordinator 中的显式和隐式迁移以及中的选项的可能性。

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

根据版本的不同,您可以通过传递 NSMigratePersistentStoresAutomaticallyOption & 来让它自动发生。 NSInferMappingModelAutomaticallyOption

还有

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

Figure out where your app stored the document and put it in the trash.

But as a extended comment you may wish to examine the possibilities around both explicit and implicit migration in NSPersistentStoreCoordinator and the options in.

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

Depending how different the versions are you can get it to happen automagically by passing NSMigratePersistentStoresAutomaticallyOption & NSInferMappingModelAutomaticallyOption

theres also

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文