Reputation: 13698
Assume, i have One-to-Many relationship between parent and childs.
How can i set child's parent by code? Something like:
[child setValue: ParentManagedObject forKey: ParentRelationship];
Where child - in child managedObject which has relationship to parent.
ParentManagedObject - is object of parent, which is fetched to singletone, when app starts
ParentRelationship - is a key in .xcdatamodeld
I know that i can create NSManagedObjectSubclass for parent and child and do it like
child.ParentRelationship = ParentManagedObject;
but i want to know how to do i without creating subclasses.
Upvotes: 1
Views: 1264
Reputation: 2027
Of course you can use KVC to get or set NSManagedObject
's properties.
For one-to-one or one-to-many relationship you can use:
[child setValue:parent forKey:parentKey];
For many-to-one or many-to-many use:
NSMutableSet *relationshipSet = [parent mutableSetValueForKey:childrenKey];
[relationshipSet addObject:child];
Where parent
and child
is NSManagedObject
instances.
Upvotes: 2