Error after adding a new core data model version

2020-04-14 06:44发布

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.

3条回答
时光不老,我们不散
2楼-- · 2020-04-14 07:18

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;
查看更多
家丑人穷心不美
3楼-- · 2020-04-14 07:42

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;
}
查看更多
该账号已被封号
4楼-- · 2020-04-14 07:44

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

查看更多
登录 后发表回答