Date
Sep. 8th, 2024
 
2024年 8月 6日

Post: Optimizing NSPredicates

Optimizing NSPredicates

Published 12:03 Mar 03, 2014.

Created by @ezra. Categorized in #Programming, and tagged as #iOS.

Source format: Markdown

Table of Content

This week I'm gonna throw three strategies you can use to improve the performance of your NSPredicates — either in Core Data or while filtering a collection like NSArray.

If things are really slow though, there are probably other places where you should be focusing, but let's assume you have already done that. ;)

Avoid Wildcards

The convenience of throwing a star wildcard (*) to look for a prefix, suffix or substring can make your predicates slower. Instead of using title LIKE "The *" use

title BEGINSWITH "The"

Instead of term LIKE *someSuffix" use

term ENDSWITH "someSuffix"

If you are looking for the occurrence of a substring within a string, don't use term MATCHES '*anything*', there is a super cool keyword exactly for that:

term  CONTAINS "anything"

Cheap Computations First

Unlike you, your iPhone's little brain is faster crunching math than processing text.

Perform the operations with numbers first (this includes NSDate) and leave the NSString comparisons at the end of the predicate.

Discard largest groups first

Place the comparisons that will eliminate the largest group of data at the beginning of the predicate. This leaves a smaller number of objects for the next comparisons (which should be the most expensive comparisons).

Pinned Message
HOTODOGO
The Founder and CEO of Infeca Technology.
Developer, Designer, Blogger.
Big fan of Apple, Love of colour.
Feel free to contact me.
反曲点科技创始人和首席执行官。
程序猿、设计师、奇怪的博主。
苹果死忠、热爱色彩斑斓的世界。
如有意向请随时 与我联系