问题描述
花了好几个小时之后,我发现在以下文章中不可能在SQLite支持的NSFetchedResultsController中进行自定义排序。
After spending hours, I found that it was not possible to do customized sort in NSFetchedResultsController backed by SQLite from following article.
但是,我找不到我问题的实际解决方案。
However, I couldn't find actual solution for my problem.
这就是我要做的事情。
背景:
我在CoreData中有一个英文字典数据库(只是一个简单的单词列表 - 非常大)。使用NSFetchedResultsController在UITableView中显示单词。
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具有关联的搜索栏。当用户在搜索栏中输入字符串时,UITableView会显示已过滤的单词列表。
UITableView has an associated Search Bar. When an user inputs a string in Search Bar, UITableView shows a list of words filtered.
为什么我需要自定义排序:
当用户输入字符串时,假设它是 bre
,我将其更改为正则表达式, b。* r。* e 。*
并将其用作NSPredicate,然后执行performFetch。所以像'bare'和'break'这样的单词将显示在表格视图中。
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.
默认情况下,单词显示为按字母顺序排列。因此,裸
将在 break
之前出现。
By default, the words are shown as a alphabetical order. Therefore, bare
will come before break
.
I希望中断
在搜索列表上裸
之前,因为的前三个字符为
与用户输入的内容完全匹配。
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.
可能的想法:
- 将NSFetchedResultsController的结果复制到NSArray并执行自定义排序。
我不确定NSArray对于像英语词典这样的大型数组有多快。 - 多次尝试performFetch。例如,请尝试执行performFetch for
bre。*
,br。+ e。*
,b。+ r。+ e。*
按顺序组合它们。
- 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.
推荐答案
您可以考虑的另一种方法是使用瞬态属性来修饰结果以指示匹配类型,然后使用该瞬态属性作为第一个排序描述符。
Another approach you could consider is using a transient property to decorate the results to indicate the type of match, and then using that transient property as the first sort descriptor.
这需要循环遍历所有结果,再次比较字符串,并设置属性。如果你期望得到一长串结果,使用数组可能同样容易。
This requires looping through all the results, comparing the strings again, and setting the attributes. If you expect a long set of results, using an array might be just as easy.
如果你需要有效地处理大型结果集,我建议使用两种排序描述符,一个只返回完全匹配的描述符,另一个只返回非完全匹配的描述符。然后显示第一个结果,然后显示第二个结果。使用复合谓词,应该可以完成。
If you need to handle large result sets efficiently, I'd suggest using two sort descriptors, one which returns only exact matches, and another which returns only non-exact matches. Then display the results from the first followed by the results from the second. With a compound predicate that should be possible to accomplish.
这篇关于NSFetchedResultsController中的自定义排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!