Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Should I use == or [NSManagedObject isEqual:] to compare managed objects in the same context?

Let's say variable A and B hold instances of managed objects in the same managed object context. I need to make sure that they are associated with the same "record" in the persistent store. The section on Faulting and Uniquing in the Core Data Programming Guide says that:

Core Data ensures that—in a given managed object context—an entry in a persistent store is associated with only one managed object.

From this, it seems that a pointer comparison is sufficient for my purpose. Or does it ever make sense to use isEqual: to compare managed objects in the same context?

like image 694
Chaitanya Gupta Avatar asked Jun 09 '11 19:06

Chaitanya Gupta


2 Answers

Use == to determine if two pointers point to the same object. Use -isEqual to determine if two objects are "equal", where the notion of equality depends on the objects being compared. -isEqual: normally compares the values returned by the -hash method. I wrote previously that it seemed possible that -isEqual: might return true if two managed objects contain the same values. That's clearly not right. There are some caveats in the docs about making sure that the hash value for a mutable object doesn't change while it's in a collection, and that knowing whether a given object is in a collection can be difficult. It seems certain that the hash for a managed object doesn't depend on the data that that object contains, and much more likely that it's connected to something immutable about the object; the object's -objectID value seems a likely candidate.

Given all that, I'm changing my opinion ;-). Each record is only represented once in a given context, so == is probably safe, but -isEqual: seems to better express your intention.

like image 53
Caleb Avatar answered Jan 03 '23 06:01

Caleb


Pointer comparison is fine for objects retrieved from a single managed object context, the documentation on uniquing you quote promises as much.

ObjectID should be used for testing object equality across managed object contexts.

isEqual does not do attribute tests, because it is documented to not fault the object. In fact, looking at the disassembled function it is definitely just a pointer compare.

So the semantics of the equality test for managed objects are simply "points to the same object (record) in the managed object context" and will compare false for objects in different contexts.

like image 25
Steven Kramer Avatar answered Jan 03 '23 05:01

Steven Kramer