Core data to-many NSPredicate with AND

蓝咒 提交于 2019-12-21 02:54:17

问题


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 name and localeIdentifier.

Any ideas for the correct syntax of this?


回答1:


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.



来源:https://stackoverflow.com/questions/2344184/core-data-to-many-nspredicate-with-and

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!