= activeFrom", som-6ren">
gpt4 book ai didi

ios - 谓词中的逻辑运算符是否与 NSFetchRequest 短路?

转载 作者:塔克拉玛干 更新时间:2023-11-02 10:21:38 29 4
gpt4 key购买 nike

我有一个使用逻辑 OR 运算符的 NSPredicate,如下所示:

NSPredicate(format: "activeFrom == NULL OR %@ >= activeFrom", someDate)

当我使用此谓词从 CoreData 获取结果时,使用 NSFetchRequest,评估是否短路,以便如果第一个子句的计算结果为真,则第二个子句不会被计算?

对于 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/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com