我有一个Core Data类“Meeting”,其中包含一个属性“date”,我希望使用NSFetchedResultsController在TableView中显示该属性.会议应按两种方式分类:首先,应在同一部分对一个月的所有会议进行总结,并按日期对一个月/部分的所有会议进行排序.
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];
代码编译没有任何错误,但排序不能按预期工作:每个会议都有自己的部分.
据我所知,排序块被完全忽略而不被调用:NSLog语句不产生任何输出,块内的断点不会停止执行.
而不是sortDescriptorWithKey:升序:比较器:我可以使用sortDescriptorWithKey:ascending:.结果完全一样.
如果块直接在sortDescriptorWithKey中声明:ascending:comparator:语句或作为变量(如上所述)也没有任何区别.
将块定义为(NSDate *,NSDate *)而不是(id,id)也没有区别.
这里出了什么问题?我会理解块内的排序是否会产生一些错误.但它怎么可能没有被称为阻止?