Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CoreData optional to-many relationships can never be nil?

Quirk I just discovered, and wanted to confirm with anyone here whether or not this is avoidable. Basically, if I have a very simple two entity model:

enter image description here

With a to-many relationship between Entity1 and Entity2. The relationship is optional, with nullify as the delete rule on both sides. However, if I insert a new Entity1 the value of the children relationship will be an empty set, not nil:

NSManagedObject *object = [NSEntityDescription
                           insertNewObjectForEntityForName:@"Entity1"
                           inManagedObjectContext:[self managedObjectContext]];
assert([object valueForKey:@"children"] != nil);

Furthermore, I can't explicitly set the relationship to nil:

[object setValue:nil forKey:@"children"];
assert([object valueForKey:@"children"] != nil);

I have verified this in a new, minimal project and this appears to be a true implementation detail. The problem is, I would like to be able to differentiate between a nil value (representing currently unknown) and an empty set (truly a to-zero relationship). Does CoreData actually support this at all in a reasonably direct manner? Currently it seems not, which means I will have to find another (less direct) way to represent my model.

Thanks,

J

like image 729
Jonathan Crooke Avatar asked Jan 28 '14 22:01

Jonathan Crooke


People also ask

Is it possible to have relationship in Coredata?

Inverse relationships enable Core Data to propagate change in both directions when an instance of either the source or destination type changes. Every relationship must have an inverse. When creating relationships in the Graph editor, you add inverse relationships between entities in a single step.

What is a core data relationship?

Persistent storage has become an essential part of the majority of iOS apps nowadays. Core Data is a persistence and/or in-memory cache framework that consists of a very powerful set of other tools that can be used within the app.


1 Answers

Short answer is no. You will always get an empty set back. If you need to know if an object structure is fully realized (my guess at what your goal is) then you would want to set an attribute on the object to say if it is fully realized or not.

like image 129
Marcus S. Zarra Avatar answered Oct 21 '22 15:10

Marcus S. Zarra