views:

160

answers:

1

I was reading the docs on core data looking to speed up my search. I found the following and was slightly confused as to what it meant:

The SQL store, on the other hand, compiles the predicate and sort descriptors to SQL and evaluates the result in the database itself. This is done primarily for performance—databases are much faster at this (it's what they're designed for)—but it means that evaluation happens in a non-Cocoa environment, and so sort descriptors (or predicates) that rely on Cocoa cannot work. The supported sort selectors are compare: and caseInsensitiveCompare:. Note that in addition you cannot sort on transient properties using the SQLite store.

the documentation is here

Does this mean that they recommend that one not use predicates or sort descriptors when fetching managed objects from an sqlite store?

I currently have a FetchRequest where I pass the following:

    NSPredicate *thingSearchPredicate = 
[NSPredicate predicateWithFormat:@"label BEGINSWITH[cd] %@", searchText];
    NSSortDescriptor *sortDescriptor = 
[[NSSortDescriptor alloc] initWithKey:@"label" ascending:YES];

The search works but I think I am using a predicate that relies on cocoa. BEGINSWITH is I assume a compare:options:range: shorthand where range is search string length. The fact that is is diacritic insensitive definitely seems to countermand the advice above so does that just mean that they are evaluated automatically for me after all instances of that entity have been fetched?

Are there any other ways to speed up search?

A: 

You will see an error if core data can't express your query in SQL. The documentation specifies that the expressions are taken and converted into SQL and passed to sqllite for performance reasons. Not that it will degenerate to Cocoa if it can't be expressed in SQL.

What they are meaning is there certainly are predicates not expressible in SQL so will not work at all.

Additionally, you need to use their sort selectors and you can't make your own, because they don't know how to covert arbitrary objects in Cocoa to SQL.

groundhog