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?
Use
==to determine if two pointers point to the same object. Use-isEqualto 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-hashmethod. 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-objectIDvalue 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.