Core data: The fetched object at index x has an out of order section name 'xxxxxx. Objects must be sorted by section name

后端 未结 2 1949
爱一瞬间的悲伤
爱一瞬间的悲伤 2021-02-05 06:43

I know I\'m not the first to ask this question but I\'m really stumped..

Basically I have a screen with two buttons. Each button loads data into a tableview below based

相关标签:
2条回答
  • 2021-02-05 06:54

    I also have got a similar issue, perhaps someone find it useful.

    I fetched financial transactions from DB and sectioned them by Month.

    DB has property trDate - which is a transaction date. But trMonth is a transient property like:

    - (NSString*)trMonth {
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"LLLL"];
    [dateFormatter setLocale:[NSLocale currentLocale]];
    
    return [dateFormatter stringFromDate:trDate];
    }
    

    It was used in FetchResultsController like this:

    ...
    NSSortDescriptor *sortDate = [[NSSortDescriptor alloc] initWithKey:@"trDate" ascending:YES];
    
    [fetchRequest setSortDescriptors:@[sortDate]];
    
    NSFetchedResultsController *aFetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:self.managedObjectContext sectionNameKeyPath:@"trMonth" cacheName:nil];
    ...
    

    Which worked well in production, but once app started to crash, and after discovering the root issue I figured out that there two transaction for (for instance) on July: July 2014 and July 2015. That was the crash point as trDate sorted well July 2014 < July 2015, but my method considered them as the same month. The solution was use as section names not only the month name but year also:

    - (NSString*)trMonthYear {
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"LLLL yyyy"]; // added year also
    [dateFormatter setLocale:[NSLocale currentLocale]];
    
    return [dateFormatter stringFromDate:trDate];
    }
    
    0 讨论(0)
  • 2021-02-05 07:00

    OK, I did take a quick peek.

    You initialize the FRC with:

    [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest
                                        managedObjectContext:self.managedObjectContext
                                          sectionNameKeyPath:@"startTime.sessionSection"
                                                   cacheName:nil];
    

    which tells it that your section titles are to be obtained via the key path startTime.sessionSection.

    Now, the first sort descriptor of a fetch request that is given to a FRC will be used to sort the sections. The sort descriptor you are providing first is for timeValue which does not seem right.

    Your first sort descriptor should specify a sort for your section titles. Change that and you may be good to go.

    EDIT

    Thanks guys for the info. I'm still a bit lost though. Did you mean that I should add a sort descriptor on startTime.sessionSection before assigning it to the sectionNameKeyPath? I tried, but still no luck. timeValue and startTime.sessionSection are related. Could that be it? – pigeonfactory

    You have to make sure that the very first sort descriptor will properly sort your data based on the section. In your case, times are being converted into words. Your initial sort descriptor is for times, and when the data is sorted based on time, the sections are not sorted properly, which is causing your error.

    The very first sort descriptor must satisfy the section data. So, initially, I would try...

    [fetchRequest setSortDescriptors:@[
        [NSSortDescriptor sortDescriptorWithKey:@"startTime.sessionSection"
                                      ascending:NO],
        [NSSortDescriptor sortDescriptorWithKey:@"timeValue"
                                      ascending:YES],
        [NSSortDescriptor sortDescriptorWithKey:@"title"
                                      ascending:YES] ];
    

    Note, if you have lots and lots of data, you may find that your section mechanism gets slow. If that happens, you may want to add this section data to your database.

    0 讨论(0)
提交回复
热议问题