基于数组排序NSFetchedResultsController结果? [英] Sort NSFetchedResultsController result based on array?
问题描述
我想构建一个基于数组内容的NSPredicate或NSSortDescriptor(核心数据搜索)。
I would like to construct a NSPredicate or NSSortDescriptor (Core Data search) that is based on the content of an array.
数组将按正确顺序由 userId:s
组成:
The array will consists of userId:s
in the right order:
[1, 2, 5, 3];
我想以相同的顺序显示我的NSFetchedResultsController的结果。
I would like to present the result for my NSFetchedResultsController in the same order.
所以我会:
- 获取正确的用户(我这样做)
- 根据我的排序数组
userId
对用户列表进行排序。
- Get the right users (I have done this)
- Sort the list of users based on my sorted array with
userId
s.
这是可以做到的,我该怎么做?
Is this possible to do, and how can I do it?
推荐答案
。对SQL请求(对于基于SQLite的存储)的结果进行排序是在SQLite级别完成的,并且只能使用存储在数据库中的持久属性。排序描述符不能使用Objective-C函数或变量。
I think that is not possible. Sorting the results of a fetch request (for a SQLite-based store) is done on the SQLite level, and can use only persistent attributes stored in the database. The sort descriptor cannot use Objective-C functions or variables.
因此,
- 您向实体添加了一个描述预期排序的附加属性(如果可能),或者
- 在获取结果后根据您的需要对结果进行排序(这意味着您不能使用提取的结果控制器进行自动更改跟踪)。也许您可以使用此处介绍的方法: http://stackoverflow.com/a/15618277/1187415 。
- you add an additional attribute to your entity that describes the intended ordering (if that is possible), or
- you sort the results according to your needs after fetching them (which means that you cannot use a fetched results controller for automatic change tracking anymore). Perhaps you can use the method described here: http://stackoverflow.com/a/15618277/1187415.
这篇关于基于数组排序NSFetchedResultsController结果?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!