0

I have an NSPredicate which looks like

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

But the performance is very very slow. Is there a way to make it easier and faster?

Cœur
  • 37,241
  • 25
  • 195
  • 267
Bashud
  • 266
  • 2
  • 8
  • 20
  • Is there a particular reason for using string attributes instead of integer/boolean ones? What are the possible values for `strasse.checks`, `raumattribute.schalter` and `strasse.standort.ortcheck`? – Nicolas Bachschmidt Apr 05 '12 at 08:56
  • strasse.checks, raumattribute.schalter and strasse.standort.ortcheck are NSSet's of bool values – Bashud Apr 05 '12 at 09:03
  • What do you run the predicate on? a fetch request or an array of objects? Where does that array come? – Nicolas Bachschmidt Apr 05 '12 at 15:04
  • I run it on a fetch request. Look at that link http://stackoverflow.com/questions/10027357/complex-predicate-with-to-many-relationship-not-working-correctly. Maybe you understand what my problem ist. its the same problem. – Bashud Apr 08 '12 at 11:17

1 Answers1

0

First, you can simplify your predicate to look like this:

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

Then, if we assume raumattribute.schalter and strasse.standort.ortcheck contain boolean values only, we can simplify the predicate again:

NSPredicate *predicate =
[NSPredicate predicateWithFormat:
 @"raumattribute.schalter.@count > 0 AND \
 (strasse.checks CONTAINS YES OR strasse.standort.ortcheck.@count > 0) OR \
 strasse.standort.ortcheck CONTAINS NO"];
Nicolas Bachschmidt
  • 6,475
  • 2
  • 26
  • 36
  • If i try your first simplify, the predicate does not filter anything. If i try the second xcode throws an exception : unsupported function expression raumattribute.schalter[SIZE] – Bashud Apr 05 '12 at 10:34
  • I replaced `[SIZE]` with `@count`. I think it should solve the exception. – Nicolas Bachschmidt Apr 05 '12 at 15:01