Objective c NSCompoundPredicate子预测顺序

Objective c NSCompoundPredicate子预测顺序,objective-c,core-data,Objective C,Core Data,用于初始化NSCompoundPredicate的数组中子谓词的顺序是否影响效率 例如: NSString * str = [self getAStr]; NSManagedObject * mo = [self getAMo]; NSString * fmt = @"(stringAttribute ==[cd] %@) AND (relationships CONTAINS %@)"; NSString * fmt2 = @"relationships.@count != 0"; NSPre

用于初始化NSCompoundPredicate的数组中子谓词的顺序是否影响效率

例如:

NSString * str = [self getAStr];
NSManagedObject * mo = [self getAMo];
NSString * fmt = @"(stringAttribute ==[cd] %@) AND (relationships CONTAINS %@)";
NSString * fmt2 = @"relationships.@count != 0";
NSPredicate * p1 = [NSPredicate predicateWithFormat:fmt,str,mo];//expensive predicate
NSPredicate * p2 = [NSPredicate predicateWithFormat:fmt2,nil];//less expensive predicate
NSCompoundPredicate * cp = [NSCompoundPredicate andPredicateWithSubpredicates:@[p1,p2];
[self fetchSomethingWithPredicate:cp];

在上面的例子中,p1和p2的顺序是否改变了复合谓词的求值方式?显然,首先计算p2会更有效,假设p2计算为false时p1将被忽略

是的,评估的顺序很重要。始终将最有效和限制性最强的谓词放在左侧-尽可能快地取消资格(尽管请记住,有些谓词非常昂贵)

如果中断调试器并打印出“cp”谓词,请确保它从左到右尽可能高效