Is there any way of using CASE
statement in SQL SELECT
using NSFetchRequest
?
@[
[NSString stringWithFormat:@"CASE %@ WHEN %@ THEN 1 WHEN %@ THEN 2 WHEN %@ THEN 3 WHEN %@ THEN 4 ELSE 0 END",
COLUMN_ENUM,
INTEGER_ENUM1,
INTEGER_ENUM2,
INTEGER_ENUM3,
INTEGER_ENUM4]
]
Where enums contains column name and integers.
I need this, because one column contains string, but I have to order by this column (not alphabetically), and I can't use SortDescriptor
with comparator (NSFetechedResultsController
not supporting it).
Any idea how can I achieve this?
A sort descriptor for a (SQL based) Core Data fetch request can only use persistent properties of the entity and only a limited set of sort selectors (compare:
, caseInsensitiveCompare:
, localizedCompare:
, localizedCaseInsensitiveCompare:
, and localizedStandardCompare:
). You cannot use Objective-C based comparators or arbitrary
SQL expressions.
localizedStandardCompare:
is useful if you have strings containing numbers that should be sorted
according to their numerical value.
(See "Fetch Predicates and Sort Descriptors" in the "Core Data Programming Guide" for more information.)
So if none of the supported sort selectors provides the ordering that you want, you will have to add an additional attribute just for ordering the objects.