I'm struggling with my predicate search here, probably a lack of vocabulary.
I have a (kind of) weak design here that my deadline doesn't allow me to change too deeply, and the situation is the following.
I'm searching in a tableview of contacts
AND users
, my two objects of concern here. The search works fine when there are only contacts (and none of the objects listed are users).
Now if it so happens (which it will, very often) that some of these contacts are users, when I search the tableview, my predicate key doesn't match, and I obviously get an exception.
How could I proceed to go around this, knowing I would like the search to still include everyone. (I have a backup plan where I just remove the users from the search feature and it works like a charm).
I tried using the OR in my predicate, like so :
@"compositeName contains[c] %@ OR name contains[c] %@" //(where %@ is my search string)
but it's not enough to skip the fact that my contact has a "compositeName" and my user has a "name", which causes an exception.
I don't want to modify the key name of my users to "compositeName" because it would imply to reinstall the app for all my beta testers, which is (due to deadlines and app-generated content) also not possible. Unless there is a way for them to have the new data model without having to reinstall, then I would do that and simply call them all "compositeName". (or "name").
I'm using core data and the array is full of NSManagedObject (of user & contact types).
Any idea?
EDIT 1 : Is this a good idea?
I'm thinking of splitting the arrays if there are users, then using different predicates in both, then finally combine them again and show those results. But it seems like a lot of processing and maybe there is something more efficient?
EDIT 2: Crash log
*** Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[ valueForUndefinedKey:]: the entity User is not key value coding-compliant for the key "compositeName".'
EDIT 3 : Class check
According to comments and other posts on stack, you can actually add a condition check, and also do class checks in predicate. Now I tried to combine both by writing this :
@"((className == %@) name contains[c] %@) OR ((className == %@) compositeName contains[c] %@)", @"User",searchText, @"Contact", searchText
which isn't a correct format. This is the first time i'm playing with predicates so if you have any clue how to write a predicate that says
(If class = User, then name contains [c], if class = Contact, then compositeName contains [c])