I have an NSManagedObject subclass and wish to fetch one of its child objects given a certain value which is a property of the child “position”.
I have written a function - (ChildObject*)childWithPosition:(int)position.
Inside here I check if that child exists and return it if it does.
If it doesn’t exist then I want to create it.
So…
Can I do this inside the ParentObject…
- (ChildObject*)childWithPosition:(int)position
{
//check if child exists and return it if it does.
// if it does not exist then...
ChildObject *child = [[ChildObject alloc] initWithEntity:[NSEntityDescription entityForName:@"ChildObject" inManagedObjectContext:self.managedObjectContext] insertIntoManagedObjectContext:self.managedObjectContext];
child.position = position;
return child;
}
My question is, is it OK and safe to use the property self.manageObjectContext on the parent and use this context to insert a new child object?
Yes. But why don’t use
managedObjectContextproperty ofNSManagedObject?From Apple docs:
So, for example: