Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to override hash and isEqual for NSManagedObjects?

We have a bunch of NSManagedObjects of various types. Some of them have members that are NSSet's of other NSManagedObjects. The problem is that I really need to override the hash and isEquals methods of the objects that are IN the set - but they are NSManagedObjects. I'm having problems with getting multiple identical objects in the set. As far as I can tell, since hash defaults to the object address - all objects are different. So I need to override hash and isEquals - but can't see any way to do it.

What we have is a bunch of stuff in the System, and more comes in via XML - sometimes repeats of the existing objects. When they are the same, I don't want dups added to the set.

like image 362
CasaDelGato Avatar asked Nov 25 '22 00:11

CasaDelGato


1 Answers

As mentioned above by Wain, NSManagedObject documentation states that you must not override hash or isEqual:. So this means a stock NSSet does not do what you need.

Some of your options are:

  • Enumerate the NSSet contents to identify and remove duplicates
  • Write a factory method for your NSManagedObjects that will return the same object when given the same inputs
  • Fix the XML to not include duplicated objects
  • Unique the objects coming from the XML before they become NSManagedObjects
  • Modify the input XML to include a unique identifier that you can track, assuming the duplicated objects are exact duplicates
  • Implement your own NSSet-like collection class that performs a different uniquing test than hash and isEqual:
like image 71
bneely Avatar answered Nov 26 '22 13:11

bneely