4

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) {                
        NSLog(@"Debug");            

        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?

Andrei Herford
  • 17,570
  • 19
  • 91
  • 225
  • You should take a look at this question: http://stackoverflow.com/questions/4789782/nsfetchedresultscontroller-custom-sort-not-getting-called The gist of it is, I don't believe comparators will work with a SQLite store in Core Data. – Paul O. Jun 13 '12 at 20:17
  • Hi Paul! Thank you very much your the hint. You are right, the comperator will not work on SQLite stores. I have added an answer to help others how might find this post – Andrei Herford Jun 14 '12 at 10:18

1 Answers1

6

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.

Andrei Herford
  • 17,570
  • 19
  • 91
  • 225