Unsupported NSSortDescriptor (comparison blocks are not supported)

In fetchedResultsController when installing the NSSortDescriptor iam of this error, an unsupported NSSortDescriptor (comparison blocks are not supported) .

 NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init]; NSEntityDescription *entity = [NSEntityDescription entityForName:@"Alarm" inManagedObjectContext: managedObjectContext]; [fetchRequest setEntity:entity]; //Below code is not working and causing error. sorting use the hours&seconds part of the time attribute NSSortDescriptor *sort = [[NSSortDescriptor alloc] initWithKey:@"time" ascending:YES comparator:^NSComparisonResult(id obj1, id obj2) { NSCalendar *calendar = [NSCalendar currentCalendar]; NSDateComponents *components1 = [calendar components:(NSHourCalendarUnit|NSMinuteCalendarUnit) fromDate:obj1]; NSDateComponents *components2 = [calendar components:(NSHourCalendarUnit|NSMinuteCalendarUnit) fromDate:obj2]; NSDate *date1 = [calendar dateFromComponents:components1]; NSDate *date2 = [calendar dateFromComponents:components2]; return [date1 compare:date2]; }]; 
+8
ios nsfetchedresultscontroller nssortdescriptor
source share
2 answers

You cannot use sort descriptors with all comparator blocks β€” for example, not by fetching master data.

They work great when filtering normal arrays.

Also - was there a question that I forgot?

+8
source share

As Monolo said, you cannot use the comparator block with Core Data. However, you can use:

 [NSSortDescriptor sortDescriptorWithKey:(NSString *) ascending:(BOOL) selector:(SEL)] 

You will need to expand the model if you are not using a standard selector.

For example, I just needed to order the lines "a la Finder" (ie 1,2,9,10, not 1,10,2,9), and I used

 request.sortDescriptors = @[[NSSortDescriptor sortDescriptorWithKey:myEntity ascending:NO selector:@selector(localizedStandardCompare:)]]; 

Take a look at Apple's NSSortDescriptor Class Link

Happy coding :)

+3
source share

All Articles