I have the following simple relationship:
Parent has many Children (ordered)
Child belongs to Parent.
And I get a strange behaviour:
// In of my classes, I keep a reference to a child.
@interface Foo ()
{
Child *_child;
}
// Somewhere in my code I create a child and a parent and associate them.
Child *c = (Child *)[NSEntityDescription insertNewObjectForEntityForName:@"Child" inManagedObjectContext:moc];
Parent *p = (Parent *)[NSEntityDescription insertNewObjectForEntityForName:@"Parent" inManagedObjectContext:moc];
[p addChildrenObject:c];
c.parent = p;
_child = c;
// Then somewhere else I do:
Parent *parent = _child.parent; // It works fine
NSOrderedSet *children = parent.children; // Same, I do see the children
int idx = [children indexOfObject:_child]; // idx is NSNotFound!!
What I can see is children
contains childs with normal IDs, whereas my _child reference still has a temporary ID.
I am using the same context everywhere.
I guess I am doing something wrong with my references, but I am not sure what it is?