2

私はコアデータでこの問題にぶつかっていますが、それは単純なはずなので、私を狂わせています

私は現在、このアプリの最初のリリースに取り組んでいます。もちろん、あちこちでコア データ モデルを微調整し続けています。

ただし、コア データ モデルを変更するたびに、アプリケーションをアンインストールして新しいバージョンを再インストールする必要があります。

これは私だけの間はまずまずですが、リリースされたら、ユーザーが再インストールせずにアプリを更新できるようにする必要があります。

私は何が欠けていますか、

既存の永続データを新しいデータに変更する方法をコアデータに伝えるために書く必要があるコードはありますか?

ご協力いただきありがとうございます

ジェイソン

4

2 に答える 2

6

コア データ モデル - 移行 - 現在のデータ モデルに新しい属性/フィールドを追加 - シミュレーターまたはアプリのリセットは不要

手順:

1) エディターからモデル バージョンを作成します - ModelVersion2のような意味のある名前を付けます

2) そのモデル バージョンに移動し、モデルに変更を加えます。

3) 次に、YourProjectModel.xcdatamodeldに移動し、現在のバージョンを新しく作成されたバージョンに設定します。

4) 永続コーディネーターを作成する場所に以下のコードを追加します -

NSDictionary *options = [NSDictionary dictionaryWithObjectsAndKeys:

[NSNumber numberWithBool:YES], NSMigratePersistentStoresAutomaticallyOption, 

[NSNumber numberWithBool:YES], NSInferMappingModelAutomaticallyOption, nil];

オプション値をメソッドのオプションとして設定します-

[__persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeURL options:nil error:&error]

私の場合、次のようになります。

- (NSPersistentStoreCoordinator *)persistentStoreCoordinator
{

 if (__persistentStoreCoordinator != nil) {
    return__persistentStoreCoordinator;
 }

 NSDictionary *options = [NSDictionary dictionaryWithObjectsAndKeys:

 [NSNumber numberWithBool:YES],      NSMigratePersistentStoresAutomaticallyOption,

 [NSNumber numberWithBool:YES], NSInferMappingModelAutomaticallyOption, nil];


 NSURL *storeURL = [[self applicationDocumentsDirectory] URLByAppendingPathComponent:@"LGDataModel.sqlite"];

  NSError *error = nil;
  __persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:[self managedObjectModel]];
  if (!   [__persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeURL options:options  error:&error])
 {
    NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
    abort();
  }

  return__persistentStoreCoordinator;
 }

リンク: http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/CoreDataVersioning/Articles/vmInitiating.html#//apple_ref/doc/uid/TP40004399-CH7-SW1

于 2013-06-14T14:02:45.307 に答える
3

Core Data のバージョニングと移行について読む必要があります。これをよく説明しているブログ投稿は次のとおりです。

http://www.timisted.net/blog/archive/core-data-migration/

于 2012-04-29T17:25:34.080 に答える