クロス-モデルに関すNSManagedObjectModelから合併。
-
02-07-2019 - |
質問
できるモデルの関係事業体で規定されている別のNSManagedObjectModels場合の主体をいれていますが、NSManagedObjectModelるための統合に関連する。
たとえば、モデルの1を定義します者 Foo
との関係(一) toBar
このモデルの2実体を定義します Bar
との関係(一) toFoo
.を構築しますCoreDataスタックを使用 -[NSManagedObjectModel mergedModelFromModels]
, 結合モデルを1モデル2.がありますこれらの関係のいずれかのデータモデラーまたはprogramaticallyようとして振る舞うしたモデルとの関係?
解決
いモデルも1モデル2は日本をはじめとする幾つかの機能をローダブ実行時にいいものを形成する場合を除き、 toBar
や toFoo
関係しています。さらに、モデルの1モデル2では同じ名前のモデルにすることはできませんで統合モデルから、いをするのではなく凝集した、衝突し、エラーになります。
しかし、利用できる NSManagedObjectModel
APIは手に負荷モデルを作成し新たなモデルの手による主体からです。の NSEntityDescription
や NSPropertyDescription
クラス(およびそのサブクラス)を実施 NSCopying
プロトコルではほとんどの場合だけコピーはでき性質から各コンポーネントモデルの総合的なモデルです。
さらに、 NS*Description
授業は全ての支援 userInfo
辞書を編集できるXcodeのデータモデリングツールを利用できないタグの関係としてスタンドです。例えば、モデル1き Bar
法 userInfo
キー MyRealEntity
チェックが作成統合モデルとして、信号の実体です。
しょうまたいスタンド-逆転の関係を立体;これらが取得できることを条件としてリアル方式が逆後の統合.のあいだに完全に複製お立体の全てのモデルもの;する必要があるのは、逆の関係に使われるリアルモデルを立てる。
この場合の実 Foo
は name
属性とリアルバーが kind
属性は、スタンドに Foo
や Bar
なので、立型に toBar
や toFoo
関係。
こちらは一部のコードを私がお話:
- (NSManagedObjectModel *)mergeModelsReplacingDuplicates:(NSArray *)models {
NSManagedObjectModel *mergedModel = [[[NSManagedObjectModel alloc] init] autorelease];
// General strategy: For each model, copy its non-placeholder entities
// and add them to the merged model. Placeholder entities are identified
// by a MyRealEntity key in their userInfo (which names their real entity,
// though their mere existence is sufficient for the merging).
NSMutableArray *mergedModelEntities = [NSMutableArray arrayWithCapacity:0];
for (NSManagedObjectModel *model in models) {
for (NSEntityDescription *entity in [model entities]) {
if ([[entity userInfo] objectForKey:@"MyRealEntity"] == nil) {
NSEntityDescription *newEntity = [entity copy];
[mergedModelEntities addObject:newEntity];
[newEntity release];
} else {
// Ignore placeholder.
}
}
}
[mergedModel setEntities:mergedModelEntities];
return mergedModel;
}
この作品なのでコピー NS*Description
物の核データの名前でより価値ある関係の先企逆や、エンティティのsubentitiesもいらっしゃいます)。このようなモデルが変更可能な、前設定されているモデル NSPersistentStoreCoordinator
使用可能なようなこの打モデルを複数のモデルです。