基于数组排序NSFetchedResultsController结果? [英] Sort NSFetchedResultsController result based on array?

查看:112
本文介绍了基于数组排序NSFetchedResultsController结果?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想构建一个基于数组内容的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.

所以我会:


  1. 获取正确的用户(我这样做)

  2. 根据我的排序数组 userId 对用户列表进行排序。

  1. Get the right users (I have done this)
  2. Sort the list of users based on my sorted array with userIds.

这是可以做到的,我该怎么做?

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆