NSManagedObjectModel中的跨模型关系从合并模型? [英] Cross-model relationships in NSManagedObjectModel from merged models?
问题描述
如果实体总是在通过合并相关模型创建的NSManagedObjectModel中使用,那么是否可以对在单独的NSManagedObjectModel中定义的实体之间的关系进行建模?
Is it possible to model relationships between entities that are defined in separate NSManagedObjectModels if the entities are always used within an NSManagedObjectModel that is created by merging the relevant models?
例如,模型1定义了一个具有关系(一对一) toBar
的实体 Foo
定义具有关系(一对一) toFoo
的实体 Bar
。我将使用 - [NSManagedObjectModel mergedModelFromModels]
构建一个CoreData堆栈,合并模型1和模型2.是否有任何方式在数据建模器或程序中定义这些关系,
For example, say model 1 defines an entity Foo
with relationship (one-to-one) toBar
and that model 2 defines an entity Bar
with a relationship (one-to-one) toFoo
. I will build a CoreData stack using -[NSManagedObjectModel mergedModelFromModels]
, merging model 1 and model 2. Is there any way to define these relationships either in the data modeler or programatically so that they behave as if they were in-model relationships?
推荐答案
模型1和模型2都不能在运行时加载,除非它们形式良好 - 也就是说,除非 toBar
和 toFoo
关系有目的地。此外,如果模型1和模型2具有相同命名的模型,您将无法从它们创建合并模型;
Neither model 1 nor model 2 will be loadable at run time unless they're well-formed — that is, unless the toBar
and toFoo
relationships have destinations. Furthermore, if model 1 and model 2 have identically-named models, you won't be able to create a merged model from them; they will not be coalesced, they will collide, which is an error.
但是,您可以使用 NSManagedObjectModel
,因为它们不会合并, API手动加载每个模型并手动创建包含来自两者的实体的新模型。 NSEntityDescription
和 NSPropertyDescription
类(及其子类)会实现 NSCopying
协议,因此在大多数情况下,您应该只能将属性从每个组件模型复制到整体模型。
However, you can use the NSManagedObjectModel
API manually to load each model and create a new model by hand that contains entities from both. The NSEntityDescription
and NSPropertyDescription
classes (and its subclasses) do implement the NSCopying
protocol so in most cases you should just be able to copy properties over from each component model to your overall model.
此外, NS *描述
类都支持可以在Xcode的数据建模工具中编辑的 userInfo
字典,您可以使用它来执行标记目标的关系作为替身。例如,在模型1中,你可以有一个 Bar
实体和 userInfo
键 MyRealEntity
,并在创建合并模型时检查是否为使用真实实体的信号。
Furthermore, the NS*Description
classes all support a userInfo
dictionary that you can edit in Xcode's data modeling tool, which you can use to do things like tag the destination of a relationship as a stand-in. For example, in model 1 you could have a Bar
entity with a userInfo
key MyRealEntity
and check for that when creating your merged model, as a signal to use the real entity instead.
与您的替代实体相反;这些将在合并后被真实反转替换。你不必在所有模型中完全复制你的stand-in实体;
You'll also want to put stand-in inverse relationships to your stand-in entities; these will be replaced with real inverses after merging. You don't have to totally replicate your stand-in entities in all models, though; you only need the inverse relationships used in your real model in a stand in entity.
因此,如果你的真实 Foo
有一个 name
属性,你的真实Bar有一个 kind
属性,你的替换 Foo
和 Bar
不需要那些,只需站在 toBar
和 toFoo
关系。
Thus if your real Foo
has a name
attribute, and your real Bar has a kind
attribute, your stand-in Foo
and Bar
won't need those, just stand-in toBar
and toFoo
relationships.
这里是一些代码演示我在说什么:
Here's some code demonstrating what I'm talking about:
- (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 * code> Core Data中的对象相对于关系的目标实体和逆(以及实体的子实体)是按名称而不是按值。因此,虽然模型是可变的 - 也就是说,在它被设置为
NSPersistentStoreCoordinator
的模型之前,您可以使用类似这样的技巧将您的模型分成多个模型。
This works because copying of NS*Description
objects in Core Data is by-name rather than by-value with respect to a relationship's destination entity and inverse (and to an entity's subentities as well). Thus while a model is mutable — that is, before it's set as the model for an NSPersistentStoreCoordinator
— you can use tricks like this to break your model into multiple models.
这篇关于NSManagedObjectModel中的跨模型关系从合并模型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!