What I have and want:
I have a one-to-many relationship A <--->> B (the to-many part is ordered).
- When deleting A all B's with a relation to A should be deleted as well, so the deletion rule for A's relation to B is set to cascade -> Works fine
- When deleting B only the relationship back to A should be cleared, so the deletion rule for B's relation to A is set to nullify -> Doesn't work (only after a delay)
Description of the problem:
So I have the exact same problem as stated in this question "Core Data Nullify rule doesn't work?": I delete a B that has a relation to an A and immediately after that, I count the number of remaining B's, that A has a relation with and it is the same as before. The accepted answer in that question was to use cascade instead of nullify since what nullify does is:
Nullify sets the pointer to null when the object is deleted. If you have an array of pointers it doesn't remove it, it just sets it to null.
I see 2 issues with that answer:
- I am pretty sure cascade is the wrong rule in this case, because it would also delete A when deleting B, which is not what I want to achieve. (I tried it nevertheless and the result was what I expected: A was also deleted).
- A collection can not have null as one of its elements, except one uses the NSNull singleton. So I doubt that this is what the nullify rule does.
After experimenting a bit I found out that, if I delete an instance of B it is deleted immediately but the relation to A is not cleared immediately but only after a little delay:
// Method is called by pressing a button
-(void)removeLastBOfA:(A *)instanceOfA
{
// Prints 4
NSLog(@"fetch all b's count before:%d", [context fetchAllBs].count);
// Prints 4
NSLog(@"A's relation to B count before: %d", instanceOfA.relationToB.count);
[context deleteObject:[instanceOfA.relationToB lastObject]];
// Prints 3
NSLog(@"fetch all b's count after:%d", [context fetchAllBs].count);
// Prints 4, but should be 3. Last Object of A's relationToB is still the object that was deleted
NSLog(@"A's relation to B count after: %d", instanceOfA.relationToB.count);
}
Now when pressing the button to call the method above again without doing anything in between, suddenly the relation is updated and "A's relation to B count before: 3" is printed. So the nullify deletion rule does work as I want it to, but with a little delay.
Questions:
- Are the 2 issues I stated valid?
- Why does nullify only update the relation after a delay and what is that delay? Or at which point do the relations get updated after deleting a NSManagedObject?
Dangling reference to an invalid object.=null
I guess thisnull
object is the object I just delete but which is still in the relationship. – Colas