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?