views:

260

answers:

2

I'm working on a Cocoa-Touch app, it uses CoreData and has some NSPersistentObject subclasses generated by the XCode model editor.

I've noticed that recently, when saving the context I get an error which has as user info the following part:

(gdb) po ui {
    "Dangling reference to an invalid object." = <null>;
    NSAffectedObjectsErrorKey = <dump #1 of someObject>;
    NSLocalizedDescription = "Operation could not be completed. (Cocoa error 1550.)";
    NSValidationErrorKey = <someKey pointing to someObject #1>;
    NSValidationErrorObject = <dump #2 of someOtherObject which points to the dump #1 object>;
    NSValidationErrorValue = { <list of someMoreObjects> };
}

There are on other keys in the user info dictionary.

All the dumps I get are valid objects, I've verified them all.

NSValidationErrorObject is an object which has an NSSet which contains objects of types dumped in NSAffectedObjectsErrorKey and NSValidationErrorValue. But all these objects are valid.

Furthermore, this happens randomly, sometimes it happens, sometimes not, and sometimes the list dumped in NSValidationErrorValue is longer, sometimes shorter.

I have no clue why this is happening, or what exactly the error is. Does anyone know what's going on? Any ideas what might cause this bizarre error?

+2  A: 

This error usually arises because a relationship is set improperly often when an object is left without a necessary reciprocal relationship. The object is "dangling" because the object graph says it should be in a relationship but it is just hanging off in space unconnected to any other object. The object is still valid in the sense that it is internal consistent but it's not in a valid place in the graph.

TechZen
The thing is, that I have three objects, A,B,C. A has many B's (NSSet). and both A and B each may have one C (relation). But I have not created a reverse relation in C, because I want to use the exact same C for both A and B and it seemed cleaner. Core data does give a warning about this but it worked when I tested and from what I read it's only a problem if I plan to do cascading automatic deletes. Do you think this could be the problem?
Prody
Possibly, but this is one of those problems whose resolution is in the details. The warning probably is telling you the source of the problem. With complex data structures, you should treat warnings as errors because the warning is usually telling you something might fail if the right circumstances line up. Check that both A and B have the same delete rule for C. In the error dump which objects are which i.e. is "someObject" and "someOtherObject" of class A,B or C?
TechZen
I've created dummy relations in C to A and B, set the reverse relations properly and set the delete actions to cascade. Now there are no more warnings, but I still get the same error :(
Prody
Btw the NSValidationErrorObject is of type C, and NSAffectedObjectsErrorKey and NSValidationErrorValue are both the same instance of B.
Prody
I've messed around with everything and I'm not exactly sure why this was happening, or why it stopped happening, but it's gone now. Since there's no other relevant answer, I'll mark this as accepted even tho it's not really the good answer. Thanks for trying tho :)
Prody
No idea how you fixed it? I would be interested.
Kamchatka
+1  A: 

Let's say you have a table "recipes" and a child table "ingredients". You then create a one-to-many relation from recipe's to ingredients and also create an inverse relationship (one-to-one) from ingredients to recipes. It makes sense to specify a delete rule of "cascade" from the recipes table because if you delete a recipe the ingredient should also be deleted. However, if you specify "no action" in the delete rule on the one-to-one relationship in ingredients you will get the dangling reference error when you try to delete an ingredient. Change the delete rule on the one-to-one relationship to "nullify" and this should correct the problem.

Chris