2012-03-03 4 views
2

古いデータを気にしないときにコアデータを新しい移行にマッサージするのに多くの時間を費やしています。私がデータモデルを変更するたびにマッピングモデルの面倒を扱うのではなく、既存のデータをすべて削除して新しいデータモデルにジャンプする方法はありますか?Core Dataを使用した痛みを伴う移行をスキップし、新しいデータモデルに移動

答えて

5

はい、店舗ファイルを削除して再作成してください。私は頻繁に(少なくとも、開発中)私のコードは、自動移行を試してみて、それが失敗した場合は、ストアを吹き飛ばして、最初からやり直しています

// storefile is an NSURL to the store file, mom is the NSManagedObjectModel 
NSError *err = nil; 
NSPersistentStoreCoordinator *psc = [[[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:mom] autorelease]; 
[psc addPersistentStoreWithType:NSSQLiteStoreType 
        configuration:nil 
          URL:storefile 
         options:[NSDictionary dictionaryWithObjectsAndKeys: 
           [NSNumber numberWithBool:YES], 
           NSMigratePersistentStoresAutomaticallyOption, 
           [NSNumber numberWithBool:YES], 
           NSInferMappingModelAutomaticallyOption, 
           nil] 
          error:&err]; 
if (err) // could be more specific about testing this error 
{ // assume automigration failed, blow away the store and try again 
    err = nil; // log it first! 
    [[NSFileManager defaultManager] removeItemAtURL:storefile 
              error:nil]; 
    [psc addPersistentStoreWithType:NSSQLiteStoreType 
        configuration:nil 
           URL:storefile 
          options:nil 
          error:&err]; 
} 
// then test err again and give up if there's still a problem 
+0

ヘクタールああ、これは、おかげで動作します。開発中、私はしばしばアプリケーションを削除しますが、これは良い方法ではありません:) – beeudoublez

+0

このコードを問題なく出荷しましたか?私は同様のアプローチを使用して私のアプリを提出しようとしている。 – kukudas

+0

郵便番号。私は(少なくとも類似のコード)私は認めなければならない。アプリケーションは、必要に応じてデータベースを素早く再構築できるようになっており、そうすることは、マッピングモデルを設定するよりも早くて安い。 – rgeorge

関連する問題