After spending hours, I found that it was not possible to do customized sort in NSFetchedResultsController backed by SQLite from following article.
NSFetchedResultsController custom sort not getting called
However, I couldn't find actual solution for my problem.
Here's what I am trying to do.
Background:
I have a English dictionary database (just a simple list of words - very large) in CoreData. Words are shown in UITableView using NSFetchedResultsController.
UITableView has an associated Search Bar. When an user inputs a string in Search Bar, UITableView shows a list of words filtered.
Why do I need customized sort:
When a user inputs a string, let's say it was bre
, I change it to regular expression, b.*r.*e.*
and use it as a NSPredicate, then, do performFetch. So that all words like 'bare' and 'break' will be shown in the table view.
By default, the words are shown as a alphabetical order. Therefore, bare
will come before break
.
I want break
to come before bare
on the searched list because the first three character of break
exactly matches to what the user input.
Possible ideas:
- Copy the result of NSFetchedResultsController into NSArray and do the custom sort. I am not sure how fast NSArray will work for a large array like an English dictionary.
- Try performFetch multiple times. For example above, try performFetch for
bre.*
,br.+e.*
,b.+r.+e.*
in order and combine them.
Both ideas don't look very neat.
I appreciate if you can suggest any known neat & typical solution for this kind of problem.