سؤال

I am using a FetchedResultsController to perform my fetch and I want it to sort the objects in descending order. If I use a custom comparator block when I create the NSSortDescriptor, I get the error saying 'unsupported NSSortDescriptor (comparator blocks are not supported)'. I don't want to fetch my items and store them in an array, sort them, and then use them because that defeats the purpose of using the FetchedResultsController. How can I create a NSSortDescriptor that will sort by descending order?

Also, why is there even a method in the framework for NSSortDescriptor that allows the use of a comparator block if it doesn't actually allow it to be used? Or is it just that you cannot use them for fetchrequests?

هل كانت مفيدة؟

المحلول

NSSortDescriptor's block based method is not supported with sqlite stores. The persistent store documentation (https://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/CoreData/Articles/cdPersistentStores.html) has this:

The SQL store, on the other hand, compiles the predicate and sort descriptors to SQL and evaluates the result in the database itself. This is done primarily for performance, but it means that evaluation happens in a non-Cocoa environment, and so sort descriptors (or predicates) that rely on Cocoa cannot work

You will have to use -[NSSortDescriptor sortDescriptorWithKey:ascending:] method.

The block based method can be used for sorting arrays and other in memory objects and I think from memory a binary Core Data store as the objects are technically also in memory

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top