2011-04-06 11 views
2

新しいモデルバージョンを追加しましたが、その新しいバージョンを使用するためにコアデータモデルを設定しましたが、アプリケーションが起動しようとするとこのエラーが発生します。新しいコアデータモデルバージョンを追加した後のエラー

"永続ストアを開くために使用された管理オブジェクトモデルのバージョンが、永続ストアの作成に使用されたものと互換性がありません。"

enter image description here

私はこの問題を推測している現在の持続ストアは、モデルの古いバージョンであるということです。それを削除して新しいものにする方法はありますか?私はそのデータの保存について気にしません。

答えて

7

バージョン間で移行する必要があります。アップルの文書によれば、変更が単純ならば、軽量移行を行うことができます。 NSPersistentStoreCoordinatorにこれらのオプションを追加

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

が動作するように見えました。

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

バージョンではあなたはそれが起こるのを取得することができますどのように異なっによっては自動的

0をNSMigratePersistentStoresAutomaticallyOption & NSInferMappingModelAutomaticallyOption

theresのを渡すこと

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; 
} 
関連する問題