核心数据:索引x处的提取对象的乱序部分名称为'xxxxxx'。 对象必须按节名称sorting

我知道我不是第一个问这个问题,但我真的很难过

基本上我有一个两个button的屏幕。 每个button都根据date将数据加载到下面的tableview中。 在第一个tableview的第一个加载(默认情况下select左边的button),一切都显示正常。 如果我点击右键,我得到一个空白的桌面,我得到的错误

在索引x处获取的对象的乱序部分名称为“xxxxxx”。 对象必须按节名称sorting。

切换回左侧的表格视图,数据不见了。 这两个表都是空的。

每个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 { //other viewDidLoad stuff [self fetchSessions]; } 

方法,该方法根据date从左侧和右侧buttonsorting数据:

 - (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]); } } 

取得结果控制器

 - (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; } 

任何帮助表示赞赏!

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

您使用以下命令初始化FRC:

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

告诉它你的部分标题是通过关键pathstartTime.sessionSection获得的。

现在,给予FRC的获取请求的第一个sorting描述符将被用来sorting这些段。 您首先提供的sorting描述符是timeValue ,看起来不正确。

您的第一个sorting描述符应该为您的节标题指定一个sorting。 改变这一点,你可能会很好去。

编辑

感谢大家的信息。 我仍然有点失落。 你是否意味着我应该在startTime.sessionSection上添加一个sorting描述符,然后将其分配给sectionNameKeyPath? 我试过了,但还是没有运气。 timeValue和startTime.sessionSection是相关的。 那可以吗? – 鸽子工厂

您必须确保第一个sorting描述符将根据该部分对数据进行正确sorting。 就你而言,时代正在转化为文字。 您的初始sorting描述符是时间,当数据按时间sorting时,部分sorting不正确,导致您的错误。

第一个分类描述符必须满足分节数据。 所以,最初,我会尝试…

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

请注意,如果您有大量的数据,您可能会发现您的部分机制变得缓慢。 如果发生这种情况,您可能需要将这部分数据添加到数据库中。

我也有类似的问题,也许有人觉得它有用。

我从DB获取财务交易,并按月分类。

DB有财产trDate – 这是一个交易date。 但trMonth是一个瞬态属性,如:

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

它被用在像这样的FetchResultsController中:

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

在生产中运行良好,但是一旦应用程序开始崩溃,在发现根本问题后,我发现在7月份(7月份)有7次交易:7月份和7月份。 2014年<2015年7月,但我的方法认为他们是同一个月。 该解决scheme被用作部分名称不仅是月份名称,而且年份也是:

 - (NSString*)trMonthYear { NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; [dateFormatter setDateFormat:@"LLLL yyyy"]; // added year also [dateFormatter setLocale:[NSLocale currentLocale]]; return [dateFormatter stringFromDate:trDate]; }