NSPredicate性能非常慢

时间:2012-04-05 08:39:18

标签: xcode core-data nspredicate

我有一个看起来像

的NSPredicate

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)"];

但表现非常缓慢。有没有办法让它更容易,更快?

1 个答案:

答案 0 :(得分:0)

首先,您可以将谓词简化为:

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"];

然后,如果我们假设raumattribute.schalterstrasse.standort.ortcheck仅包含布尔值,我们可以再次简化谓词:

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"];