Core data to-many NSPredicate with AND
I'm trying to write a query for the find-as-you-type search bar. What I want to do is query "Kind", and return any Kinds for which there is a LocalName with ('name' LIKE %@ AND localeIdentifier == %@).
If I'm only searching the names (so ignoring the localeIdentifier), I could do something like this:
ANY localized.name LIKE %@
What I want is something more like
ANY localized.(name LIKE %@ AND localeIdentifier == %@)
To sum up, searching "Kind", any one item in the to-many relationship "localized" should match both nam开发者_如何学Pythone and localeIdentifier.
Any ideas for the correct syntax of this?
What you want is a subquery. In predicate format syntax:
SUBQUERY(self.localized, $x, $x.name LIKE %@ AND $x.localeIdentifier == %@).@count > 0
where the SUBQUERY
expression returns a collection of instances in the self.localized
collection that match the predicate in the third argument. Kind
instances for which this SUBQUERY expression is non-empty (ie @count > 0
) match your desired criteria.
The SUBQUERY expression was introduced in OS X 10.5.
精彩评论