19

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

Jonathan Crooke
  • 912
  • 7
  • 19

1 Answers1

18

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.

Marcus S. Zarra
  • 46,571
  • 9
  • 101
  • 182