NSSortDescriptor ignores Comperator Block

I have a Core Data class "Meeting" with a property "date" which I want to display in a TableView using a NSFetchedResultsController. The meetings should be sorted in two ways: At first all meetings of a month should be summerized in the same section and all meetings in a month/section should be sorted by date.

    NSComparisonResult (^periodSortBlock)(id, id) = ^(id obj1, id obj2) {                

        NSDate *date1 = (NSDate *)obj1;
        NSDate *date2 = (NSDate *)obj2;

        // Pseudocode
        Month *month = [Month monthWithDate:date1];
        NSComparisonResult result = NSOrderedSame;

        if ([Month date:date2 afterDate:month.end])
            result = NSOrderedAscending;
        else if ([Month date:date2 beforeDate:month.start])
            result = NSOrderedDescending;

        return result;

    NSFetchRequest *fetchRequest = [NSFetchRequest fetchRequestWithEntityName:@"Meeting"];        
    fetchRequest.predicate = [NSPredicate predicateWithFormat:@"SELF IN %@", self.meetings];

    NSSortDescriptor *sectionByMonth = [NSSortDescriptor sortDescriptorWithKey:@"date" ascending:false comparator:periodSortBlock];
    NSSortDescriptor *sortByDate = [NSSortDescriptor sortDescriptorWithKey:@"date" ascending:false];

    fetchRequest.sortDescriptors = [NSArray arrayWithObjects:sectionByMonth, sortByDate, nil];

    frc = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:self.managedObjectContext sectionNameKeyPath:@"date" cacheName:nil];
    [frc performFetch:nil];

The code compiles without any error but the sorting does not work as expected: Every meeting is has its own section.

As far as I can see the sorting block is completely ignored and not called: The NSLog statement does not produce any output and an breakpoint within the block does not stop the execution.

Instead of sortDescriptorWithKey:ascending:comparator: I could use sortDescriptorWithKey:ascending:. The result is exactly the same.

If the block is declared directly in the sortDescriptorWithKey:ascending:comparator: statement or as a variable (like above) does not make any difference as well.

Defining the block as (NSDate*, NSDate*) instead of (id, id) is also no difference.

What is going wrong here? I would understand if the sorting within the block would produce some errors. But how is possible that the block it not called?


Thanks to the hint from Paul I was able to figure out what the problem is. The solution can be found in the Apple docs (have a look at the "Fetch Predicates and Sort Descriptors" section).

I thought the the NSFetchRequest would just use SQL to fetch the Objects from the SQLite DB and then use the SortDiscriptors to sort the fetched objects in memory. This is not true: It seems that the complete sorting work is handed over to the SQLite store as well. Of course the Obective-C statesments form the block cannot be translated into a SQL query and thus the block is ignored.

Need Your Help

Get all calendar events for a specific user

calendar outlook exchange-server exchangewebservices

As part of creating a service for syncing an internal application's calendar with Outlook Calendar (Exchange) I need a way to get all of the events from the calendars of a list of users. Through EW...

Samsung Smart TV App development - Show notification on screen

javascript node.js samsung-smart-tv

I am trying to develop an application on samsung smart tv which can display notification while you are watching TV. a notification will contain information about soccer games, twitter messages (can...