核心数据:索引x处获取的对象具有无序区段名称'xxxxxx。对象必须按节名称排序 [英] Core data: The fetched object at index x has an out of order section name 'xxxxxx. Objects must be sorted by section name

查看:136
本文介绍了核心数据:索引x处获取的对象具有无序区段名称'xxxxxx。对象必须按节名称排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道我不是第一个提出这个问题,但我真的很累。



基本上我有一个有两个按钮的屏幕。每个按钮根据日期将数据加载到下面的表格中。在第一次加载第一个tableview(左边的按钮被默认选择)一切显示正常。如果我点击正确的按钮,我得到一个空白的tableview,我得到的错误


索引x的获取对象有一个out的订单节名称
'xxxxxx。对象必须按节名称排序。


切换回左表视图,数据消失。两个表视图都为空。



每个tableview有2个部分,取决于项目的开始时间。如果我删除部分数据显示正常。不幸的是我需要他们..数据被分类到两个部分,如下:

  @interface NSString(agendaSessionKeyPath)
@property(nonatomic,readonly)NSString * sessionSection;
@end

@implementation NSString(agendaSessionKeyPath)

- (NSString *)sessionSection
{
int timeValue = [[self stringByReplacingOccurrencesOfString :@:withString:@] intValue]; // turns 11:00 to 1100
if(timeValue< 1200)
return @Morning;
else
return @Afternoon;
}

获取请求

   - (void)viewDidLoad 
{
//其他viewDidLoad东西
[self fetchSessions];
}

方法根据日期从左侧和右侧按钮排序数据: / p>

   - (void)fetchSessions 
{
NSDateFormatter * dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];
[dateFormatter setDateFormat:@yyyy-MM-dd];

NSDate * date = nil;
if(selected == 0)//选择左键
{
date = [dateFormatter dateFromString:@2012-09-26];
}
else if(selected == 1)//选择右键
{
date = [dateFormatter dateFromString:@2012-09-27];
}

NSPredicate * predicate = [NSPredicate predicateWithFormat:@date ==%@,date];
[self.fetchedResultsController.fetchRequest setPredicate:predicate];

NSError * error;
if(![[self fetchedResultsController] performFetch:& error]){
NSLog(@未解析的错误%@,%@,错误,[错误userInfo]);
}
}

提取结果控制器

   - (NSFetchedResultsController *)fetchedResultsController {
self.managedObjectContext = [[MATCDatabaseController sharedDatabaseController] managedObjectContext];
if(_fetchedResultsController!= nil){
return _fetchedResultsController;
}

NSFetchRequest * fetchRequest = [[NSFetchRequest alloc] initWithEntityName:@Session];

NSSortDescriptor * sort = [[NSSortDescriptor alloc]
initWithKey:@titleascending:YES];
NSSortDescriptor * timeSort = [NSSortDescriptor sortDescriptorWithKey:@timeValueascending:YES];
[fetchRequest setSortDescriptors:@ [timeSort,sort]];
[fetchRequest setFetchBatchSize:20];

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

self.fetchedResultsController = theFetchedResultsController;
[self.fetchedResultsController setDelegate:self];

return _fetchedResultsController;

}

任何帮助!

解决方案

好的,我快速地看了一眼。



初始化FRC:

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

它说明您的节标题是通过键路径 startTime.sessionSection



现在,提供给FRC的获取请求的第一个排序描述符将用于对节进行排序。您首先提供的排序描述符是 timeValue ,它似乎不正确。



您的部分标题的排序。




感谢各位的信息。我还是有点失落。你的意思是
,我应该在startTime.sessionSection之前添加一个排序描述符
之前分配给sectionNameKeyPath?我试过,但还是没有运气。
的timeValue和startTime.sessionSection是相关的。可能是吗?
- pigeonfactory


您必须确保第一个排序描述符根据节正确排序数据。在你的情况下,时间被转换成词。您的初始排序描述符是针对时间的,当数据基于时间排序时,这些部分不会正确排序,这会导致您的错误。



描述符必须满足节数据。所以,最初,我会尝试...

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

注意,如果你有很多很多数据,你可能会发现你的section机制变慢了。如果发生这种情况,您可能需要将此区段数据添加到数据库。


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 on a date. On the first load of the first tableview (the left button is selected by default) everything displays fine. If I click on the right button and I get a blank tableview, and I get the error

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

Switching back to the left table view, the data is gone. Both tableviews are empty.

Each tableview has 2 sections depending on the start time of the item. If I eliminate the sections the data displays fine. Unfortunately I need them.. the data is sorted into the two sections like so:

@interface NSString(agendaSessionKeyPath)
@property (nonatomic, readonly) NSString *sessionSection;
@end

@implementation NSString(agendaSessionKeyPath)

- (NSString *)sessionSection
{
    int timeValue = [[self stringByReplacingOccurrencesOfString:@":" withString:@""] intValue]; //turns 11:00 to 1100
    if (timeValue < 1200)
        return @"Morning";
     else
        return @"Afternoon";
}

Fetch request

- (void)viewDidLoad
{
     //other viewDidLoad stuff
    [self fetchSessions];
}

method which sorts the data from the left and right button based on date:

- (void)fetchSessions
{
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];
    [dateFormatter setDateFormat:@"yyyy-MM-dd"];

    NSDate* date = nil;
    if (selected == 0) //left button is selected
    {
        date = [dateFormatter dateFromString:@"2012-09-26"];
    }
    else if (selected == 1) //right button is selected
    {
        date = [dateFormatter dateFromString:@"2012-09-27"];
    }

    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"date == %@", date];
    [self.fetchedResultsController.fetchRequest setPredicate:predicate];

    NSError *error;
    if (![[self fetchedResultsController] performFetch:&error]) {
        NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
    }
}

fetched results controller

- (NSFetchedResultsController *)fetchedResultsController {
    self.managedObjectContext = [[MATCDatabaseController sharedDatabaseController] managedObjectContext];
    if (_fetchedResultsController != nil) {
        return _fetchedResultsController;
    }

    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] initWithEntityName:@"Session"];

    NSSortDescriptor *sort = [[NSSortDescriptor alloc]
                              initWithKey:@"title" ascending:YES];
    NSSortDescriptor *timeSort = [NSSortDescriptor sortDescriptorWithKey:@"timeValue" ascending:YES];
    [fetchRequest setSortDescriptors:@[timeSort, sort]];
    [fetchRequest setFetchBatchSize:20];

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

    self.fetchedResultsController = theFetchedResultsController;
    [self.fetchedResultsController setDelegate:self];

    return _fetchedResultsController;

}

any help is appreciated!

解决方案

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.

这篇关于核心数据:索引x处获取的对象具有无序区段名称'xxxxxx。对象必须按节名称排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆