I want to use three sections for a tableview:
New Issues = issues reported on this date regardless of status.
Active Issues = issues reported prior to this date still requiring follow up.
Closed Issues = issues reported prior to this date, not requiring follow up.
I have a transient property which correctly reports the sections – 0,1,2 respectively.
I have tried numerous ways to get my NSFetchedResultsController to sort the results correctly for the tableview to display. I find you cannot sort on the transient property in the NSFetchedResultsController so I tried to sort like so:
//create sort keys
NSDateFormatter *df = [[NSDateFormatter alloc] init];
[df setDateStyle:NSDateFormatterShortStyle];
[df setTimeStyle:NSDateFormatterNoStyle];
NSString *nowString = [df stringFromDate:[NSDate date]];
NSDate *todayDateWithoutTime = [df dateFromString:nowString];
[df release];
NSSortDescriptor *sortIsNew = [[NSSortDescriptor alloc] initWithKey:@"dateReported" ascending:YES comparator:^(id obj1,id obj2){
NSDate *date1 = obj1;
NSDate *date2 = obj2;
NSComparisonResult comparisonResult;
if ([date1 timeIntervalSinceDate:todayDateWithoutTime] == 0) {
if ([date2 timeIntervalSinceDate:todayDateWithoutTime] == 0) {
comparisonResult = NSOrderedSame;
} else {
comparisonResult = NSOrderedAscending;
}
} else {
if ([date2 timeIntervalSinceDate:todayDateWithoutTime] == 0) {
comparisonResult = NSOrderedDescending;
} else {
comparisonResult = NSOrderedSame;
}
}
return comparisonResult;
}];
NSSortDescriptor *sortByFollowUp = [[NSSortDescriptor alloc] initWithKey:@"requiresFollowUp" ascending:NO];
NSSortDescriptor *sortByReportDate = [[NSSortDescriptor alloc] initWithKey:@"dateReported" ascending:NO];
NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:sortIsNew, sortByFollowUp,sortByReportDate, nil];
[fetchRequest setSortDescriptors:sortDescriptors];
Not so sure my usage of the block is correct, but in any case it never gets called so far as I can tell. I put some log statements in there and they do not report. Apparently the “sortByFollowUp” and “sortByReportDate” descriptors work as expected, but not the “sortIsNew”. If there are no new Issues everything works fine. In any case where there are new Issues for that first section, the app crashes as the sorting does not match the sections.
I know I can do this very easily with straight up fetch requests and arrays. It only seems right to use the fetched results controller to avoid pulling the entire table of Issues into memory when most are unneeded and only a few will be displayed at any given moment. Can it really be this difficult to use it when you want custom sections?
As I have not received any answers I am closing the question with my own conclusions.
Saving the transient value as an attribute appears actually to be the simplest fix. I have experimented with variations of a combo NSFetchedResultsController and Custom array method but it seems you lose a lot of the benefits of the NSFetchedResultsController that way. You wind up having to chase down all the events received and refresh the arrays as applicable. It seems that you may as well skip the NSFetchedResultsController in that case and just go ahead and work with arrays of objects directly.