CoreData如何管理类关系创建? [英] how does CoreData manage class relationship creation?
问题描述
我有一个实体类,在托管上下文中正确定义。
这个类与另一个类有一对多的关系。
Xcode 4图形工具正确地创建了派生类,关系用一个NSSet。
我想知道如何管理关系类的创建。
我的意思是,为创建主实体我使用
NSManagedObject * newEntity = [NSEntityDescription
insertNewObjectForEntityForName:@EntityName
inManagedObjectContext:context];
但是NSSet中的关系呢?我需要以与父实体相同的方式创建它并将其定期存储在NSSet中吗?
NSManagedObject * child = [NSEntityDescription insertNewObjectForEntityForName:@ChildNameinManagedObjectContext:context];
NSSet * childSet = [..set creation with child ..];
newEntity.child = childSet;
//在上下文中保存newEntity
如果是,对象为什么不需要从上下文开始创建?这样的问题可以应用于实体中的所有正常属性,NSString也是一个对象,为什么一个简单的newEntity.prop = @就够了?
对于多对多关系NSManagedObject有一个 mutableSetValueForKey:
方法返回您将使用的集合。使用上面定义的 newEntity
和 child
,您可以这样做:
NSMutableSet * childObjects = [newEntity mutableSetValueForKey:@childRelationship];
[childObjects addObject:child];但是你说你有Xcode为你的核心数据实体生成自定义子类,所以你有一个方便的方法在 Parent
类中定义,类将被命名为 addChildObject:
。使用它,你可以用一个更简单的版本替换上面,但你还需要声明 newEntity
作为这个子类的实例,而不是一个通用的NSManagedObject: Parent * parent = [NSEntityDescription insertNewObjectForEntityForName:@EntityNameinManagedObjectContext:context];
NSManagedObject * child = [NSEntityDescription insertNewObjectForEntityForName:@ChildNameinManagedObjectContext:context];
[parent addChildObject:child];
I have an entity class, correctly defined in a managed context.
This class has a one-to-many relationship with another class.
Xcode 4 graphic facilities correctly created the derived classes, and the relationship is represented by a NSSet.
I am wondering how the creation of the relation classes is managed.
I mean, for creating the main entity I am using the
NSManagedObject *newEntity = [NSEntityDescription
insertNewObjectForEntityForName:@"EntityName"
inManagedObjectContext:context];
But what about the relationship in NSSet ? Do I need to create it in the same way as a parent entity and store it regularly in NSSet ?
NSManagedObject *child = [NSEntityDescription insertNewObjectForEntityForName:@"ChildName" inManagedObjectContext:context];
NSSet *childSet = [..set creation with child..];
newEntity.child = childSet;
// save newEntity in context
If yes, because NSSet is an object why doesn't it need to be created starting from the context ? Such question could be applied to all the 'normal' properties in the entity, an NSString is an object too, why a simple newEntity.prop=@"" is enough ?
解决方案 For to-many relationships NSManagedObject has a mutableSetValueForKey:
method that returns the set you would use. With newEntity
and child
defined as above you'd do something like this:
NSMutableSet *childObjects = [newEntity mutableSetValueForKey:@"childRelationship"];
[childObjects addObject:child];
But you said you had Xcode generate custom subclasses for your Core Data entities, so you have a convenience method defined in the Parent
class which would be named something like addChildObject:
. Using that you could replace the above with a simpler version, but you'll also need to declare newEntity
as an instance of this subclass instead of as a generic NSManagedObject:
Parent *parent = [NSEntityDescription insertNewObjectForEntityForName:@"EntityName" inManagedObjectContext:context];
NSManagedObject *child = [NSEntityDescription insertNewObjectForEntityForName:@"ChildName" inManagedObjectContext:context];
[parent addChildObject:child];
这篇关于CoreData如何管理类关系创建?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!