I would like to construct a NSPredicate or NSSortDescriptor (Core Data search) that is based on the content of an array.
The array will consists of userId:s
in the right order:
[1, 2, 5, 3];
I would like to present the result for my NSFetchedResultsController in the same order.
So I will:
- 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?
The above answer is not exactly true. You can do what you want, but it's a bit complicated.
Put a + global variable of NSArray type onto category using objc_setAssoc....
Set the array somewhere:
Use sort descriptor of type:
This will apparently work as it works for strings with localizedCaseInsensitiveCompare: However, it will degrade performance of the query if it is big enough and override any fetchLimit/fetchOffset optimizations. Remember that the field you are operationg on has to be unique, otherwise it will all go to hell.
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.
So either