我有一个使用逻辑 OR 运算符的 NSPredicate
,如下所示:
NSPredicate(format: "activeFrom == NULL OR %@ >= activeFrom", someDate)
当我使用此谓词从 CoreData 获取结果时,使用 NSFetchRequest
,评估是否短路,因此如果第一个子句评估为 true,则不会评估第二个子句?
对于 Apple 来说,这似乎是一个明智的优化,但我在文档中找不到任何官方信息来确认它。
来自 Core Data Programming Guide ,性能部分:
Fetch Predicates
How you use predicates can significantly affect the performance of your application. If a fetch request requires a compound predicate, you can make the fetch more efficient by ensuring that the most restrictive predicate is the first, especially if the predicate involves text matching (contains, endsWith, like, and matches). Correct Unicode searching is slow. If the predicate combines textual and nontextual comparisons, it is likely to be more efficient to specify the nontextual predicates first; for example, (salary > 5000000) AND (lastName LIKE 'Quincey') is better than (lastName LIKE 'Quincey') AND (salary > 5000000). For more about creating predicates, see Predicate Programming Guide.
所以,是的,谓词的评估似乎已如您所料优化。这在很大程度上取决于 SQLite 如何优化相应的 WHERE 子句(假设您使用的是 SQLite 存储)。
关于ios - 谓词中的逻辑运算符是否与 NSFetchRequest 短路?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48148709/
欢迎光临 OStack程序员社区-中国程序员成长平台 (https://ostack.cn/) | Powered by Discuz! X3.4 |