2012-11-21 18 views
1

數組中的子選項用於初始化NSCompoundPredicate影響效率嗎?NSCompoundPredicate subpredicate order

實施例:

NSString * str = [self getAStr]; 
NSManagedObject * mo = [self getAMo]; 
NSString * fmt = @"(stringAttribute ==[cd] %@) AND (relationships CONTAINS %@)"; 
NSString * fmt2 = @"[email protected] != 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將被忽略,如果p2評估爲false。

回答

2

是的,評估順序很重要。始終將最有效的限制性謂詞放在左邊 - 儘可能快地使資格儘可能不合格(但請記住,有些謂詞確實很昂貴)。

如果您在調試器中斷並打印出'cp'謂詞,請確保它儘可能高效,從左到右。