开发者

Error after adding a new core data model version

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 o开发者_JAVA技巧pen the persistent store is incompatible with the one that was used to create the persistent store."

Error after adding a new core data model version

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.


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;


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


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜