Complex predicate with to-many relationship not working correctly

一曲冷凌霜 提交于 2019-12-25 01:20:52

问题


I have an app where i can search for a room to book it.

In my first view I can select a city and the street. In the second view I can select some attributes (like wlan, tv...)

In the 3rd view are the rooms that are available to the selection.

When I don't select an attribute, it should show me all the rooms that are available for the selected street/city.

Here is my predicate so far:

   NSPredicate *predicate =
[NSPredicate predicateWithFormat:
 @"((raumattribute.schalter CONTAINS YES) AND\
 (strasse.checks CONTAINS YES OR strasse.standort.ortcheck CONTAINS YES)) \
 OR\
 ((raumattribute.schalter CONTAINS NO) AND\
 (strasse.checks CONTAINS YES OR strasse.standort.ortcheck CONTAINS YES))"];

The problem is, i can't filter the rooms anymore with the attributes. When I select the city and go to the rooms, I can see all the rooms in the city. But as soon as I select an attribute it doesn't filter the rooms, but shows all the rooms in the selected city.

When I write my predicate like this:

 NSPredicate *predicate =

[NSPredicate predicateWithFormat:
 @"((raumattribute.schalter CONTAINS YES) AND\
 (strasse.checks CONTAINS YES OR strasse.standort.ortcheck CONTAINS YES)) 

I can filter the rooms with the streets and the attributes. .But when no attribute is selected, I can't see any room in the selected street.

here is a Screenshot of my updated schema: Screenshot


回答1:


You are mistaking the core-data framework, which is an object graph, with an SQL database substitute. This can be debated ad nauseam so let's not get into a philosophical discussion.

The proper way to do this from your problem description is to filter the rooms by street and then iterate through the result to exclude those that do not have the checked attributes. The resulting array could be the datasource of your resulting table.

However, from your code examples and the German entity and attribute names I am observing, it seems that you should perhaps consider revising your data structure. If you edit your question and also post your data model scheme I can give you further comments.



来源:https://stackoverflow.com/questions/10027357/complex-predicate-with-to-many-relationship-not-working-correctly

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