Re: Core Data NSPredicate
Re: Core Data NSPredicate
- Subject: Re: Core Data NSPredicate
- From: Andreas Grosam <email@hidden>
- Date: Mon, 14 Mar 2011 16:33:29 +0100
On Mar 14, 2011, at 3:10 PM, Indragie Karunaratne wrote:
> Thanks Andreas,
>
> I see your point, but as far as I know, "IN" looks for an exact match (in this case, the attribute would have to match the term for the predicate to return true)
Well, just to clarify the things:
IN is a set operator, e.g.
e IN R
The left hand argument e is an element, and the right hand argument R is an array or set (or even a dictionary, but then the values are considered only). The predicate evaluates to TRUE if the element e is an element of R. If Objective-C code is involved to test the inclusion, I'm not sure which methods are applied, but my guess is it is equivalent to checking for each element with -isEqual. Otherwise the database backend will do the comparison, which requires an exact match.
(btw: In case of a fetch request, we might imagine that each managed object in the specified entity is test separately against the predicate)
Example:
'aaa' IN {'aaa', 'bbb', 'ccc'} => TRUE
'xxx' IN {'aaa', 'bbb', 'ccc'} => FALSE
> where as I need it to check if the attribute CONTAINS the term (which is more appropriate for Spotlight-style search). That said, I am by no means an expert with predicates so correct me if I am wrong.
CONTAINS is a "String Comparison" operator.
s1 CONTAINS s2
This predicate returns true if the left hand string contains the right hand string:
'aaaBBBccc' CONTAINS 'aaa' => TRUE
'aaaBBBccc' CONTAINS 'aaaccc' => FALSE
And finally, ALL is an "Aggregate Modifier"
ALL R1 IN R2
ALL is an aggregate operation which operates on a set or an array. Both arguments R1, and R2 are sets or arrays.
Effectively, the above is equivalent to:
(e0 IN R2) AND (e1 IN R2) ... AND (en IN R2)
where R1 = {e0, e1, ... en}
ALL {'a', 'b'} IN {'a', 'b', ācā, 'd'} => TRUE
ALL {'a', 'b', 'x'} IN {'a', 'b', ācā, 'd'} => FALSE
So, it just depends what you are actually going to achieve ;)
Your solution was to evaluate this predicate:
pred = " (name CONTAINS 'term0' OR artist.name CONTAINS 'term0') AND (name CONTAINS 'term1' OR artist.name CONTAINS 'term1') ..."
OK, this might be very well the exact thing you want to achieve - but from the logic, namely to test if all of many terms (character strings) are contained (literally) in an artist's name - it seems otherwise ;)
Andreas
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden