0

I am trying to create a NSFetchedResultsController using date and type.

My Data looks like this :

{
 @"date", NSDate,
 @"type", NSString
}

This is what my NSManagedObject

- (NSString*)dayTypeStringOnly {

    NSCalendar *cal = [NSCalendar currentCalendar];
    return [NSString stringWithFormat:@"%i%i%i%@", (int)[[cal components:NSYearCalendarUnit fromDate:self.startTime] year],(int)[[cal components:NSMonthCalendarUnit fromDate:self.startTime] month],(int)[[cal components:NSDayCalendarUnit fromDate:self.date] day], [self.type lowercaseString]];
}

Here is my NSFetchedResultsController:

NSFetchRequest *dataRequest = [NSFetchRequest fetchRequestWithEntityName:entityName];
[dataRequest setPredicate:kHiddenPredicate];
[dataRequest setFetchBatchSize:kMagicalRecordDefaultBatchSize];
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"type" ascending:NO selector:@selector(caseInsensitiveCompare:)];
[dataRequest setSortDescriptors:@[[NSSortDescriptor sortDescriptorWithKey:@"date" ascending:YES], sortDescriptor]];
NSFetchedResultsController *dataFetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:dataRequest
                                                                                               managedObjectContext:[NSManagedObjectContext MR_defaultContext]
                                                                                                 sectionNameKeyPath:@"dayTypeStringOnly"
                                                                                                          cacheName:nil];

I am getting this error:

Unresolved error Error Domain=NSCocoaErrorDomain Code=134060 "The operation couldn’t be completed. (Cocoa error 134060.)" UserInfo=0x10e847230 {reason=The fetched object at index 8 has an out of order section name '20131011pump. Objects must be sorted by section name'}, {
    reason = "The fetched object at index 8 has an out of order section name '20131011pump. Objects must be sorted by section name'";

Can anyone see what I am doing wrong here?

Tim Walsh
  • 1,089
  • 11
  • 27
  • Hint: the `dayTypeStringOnly` needs to go first in the sort descriptors, then add your other sort descriptors after. – iwasrobbed Oct 15 '13 at 02:43
  • I think the problem is that `dayTypeStringOnly`, his only attributes are `@"date"` and `@"type"` – Kevin Oct 15 '13 at 02:50
  • @Kevin He has a custom getter called `dayTypeStringOnly` in the `NSManagedObject` subclass. This is standard for creating derived properties from actual model properties like he is doing. – iwasrobbed Oct 15 '13 at 02:55
  • I was under the impression that you couldn't use derived properties to sort (I asked a question which led me to believe this - http://stackoverflow.com/questions/18558421/nspredicate-in-nsfetchedresultscontroller-doesnt-use-categorys-getter – Kevin Oct 15 '13 at 03:07
  • I stand corrected then – Kevin Oct 15 '13 at 03:13
  • I can't put dayTypeStringOnly into the sort descriptor: 'keypath dayTypeStringOnly not found in entity' – Tim Walsh Oct 15 '13 at 03:53
  • 1
    @iWasRobbed: You can use transient/derived properties as `sectionNameKeyPath` parameter, but *not* in a (SQLite based) Core Data sort descriptor. – Martin R Oct 15 '13 at 07:06
  • 1
    @TimWalsh: The problem is indeed that you have to add a first sort descriptor that is "compatible" with your `sectionNameKeyPath`. If that were just "year-month-day" than you could use the "date" attribute as first sort descriptor, but with "year-month-day-type" you are probably out of luck. You may have to add "dayTypeStringOnly" as an additional *persistent* attribute of your entity. – Martin R Oct 15 '13 at 07:10
  • @Marin you are correct, it works fine when you just a date, but once I add in the type...it fails. Looks like having another field with that data would be a clean solution. If you want to repost your answer, I will happily accept it. – Tim Walsh Oct 15 '13 at 19:21
  • The question has already been closed as a duplicate, therefore I cannot post an answer anymore. – Martin R Oct 16 '13 at 07:17

0 Answers0