Core data NSFetchedResultsController组每4小时分为多个部分

Core data NSFetchedResultsController组每4小时分为多个部分,core-data,uitableview,nsfetchedresultscontroller,nsfetchrequest,Core Data,Uitableview,Nsfetchedresultscontroller,Nsfetchrequest,我正在尝试将一系列NSManagedObject分组到各个部分。每个部分代表一天内4小时的时间段 -(NSString*)节标识符 { //按需创建并缓存节标识符。 [self-willAccessValueForKey:@“sectionIdentifier”]; NSString*tmp=[self-primitiveSectionIdentifier]; [self-didAccessValueForKey:@“sectionIdentifier”]; 如果(!tmp) { NSCalen

我正在尝试将一系列NSManagedObject分组到各个部分。每个部分代表一天内4小时的时间段

-(NSString*)节标识符
{
//按需创建并缓存节标识符。
[self-willAccessValueForKey:@“sectionIdentifier”];
NSString*tmp=[self-primitiveSectionIdentifier];
[self-didAccessValueForKey:@“sectionIdentifier”];
如果(!tmp)
{
NSCalendar*日历=[NSCalendar currentCalendar];
NSDateComponents*components=[日历组件:(NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit | NSHourCalendarUnit)fromDate:[自行创建]];
tmp=[NSString stringWithFormat:@“%.0f%d%d%d”,四舍五入(components.hour%4),components.day,components.month,components.year];
[self-setPrimitiveSectionIdentifier:tmp];
}
返回tmp;
}
在上述代码中,NSManagedObject中的transient属性
sectionIdentifier
返回一个字符串,该字符串由它所在的时间段(4小时)、创建日期、月份和年份组成

但是,尽管NSFetchedResultController的fetchRequest的排序描述符设置为按结果的创建日期(最新的对象在底部,最旧的对象在顶部)对结果进行排序,但由于节标识符的顺序不正确,因此提取请求失败

-(NSFetchedResultsController*)fetchedResultsController{
如果(!\u fetchedResultsController){
NSFetchRequest*fetchRequest=[[NSFetchRequest alloc]init];
[fetchRequest setEntity:[NSEntityDescription entityForName:@“PSSpace”inManagedObjectContext:self.managedObjectContext]];
[FetchRequestSetSortDescriptors:@[[NSSortDescriptor SortDescriptor WithKey:@“已创建”升序:是]];
_fetchedResultsController=[[NSFetchedResultsController alloc]initWithFetchRequest:fetchRequest managedObjectContext:self.managedObjectContext sectionNameKeyPath:@“sectionIdentifier”缓存名:nil];
[\u fetchedResultsController setDelegate:self];
}
返回_fetchedResultsController;
}
CoreData:错误:(NSFetchedResultsController)索引10处获取的对象具有无序的节名“1 14 12 2013”。对象必须按节名称排序'

请告诉我如何通过订购解决这个问题

谢谢。

试试类似的东西

if(!tmp){
NSCalendar*日历=[NSCalendar currentCalendar];
NSDateComponents*components=[日历组件:(NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit | NSHourCalendarUnit)fromDate:[自行创建]];
NSU整数_y=(components.year*1000000);
NSU整数_m=(components.month*10000);
NSU整数_d=(components.day*100);
NSU整数_h=四舍五入(单位:小时/4);
NSU整数标记=(_y+_m+_d+_h);
tmp=[NSString stringWithFormat:@“%lu”,令牌];
[self-setPrimitiveSectionIdentifier:tmp];
}

这实际上与我目前所做的一样。问题在于我在哪里计算出日期在4的哪个时段。我需要用其他东西替换组件。小时%4以计算周期。@max\ux:这不一样。此代码(几乎)生成一个格式为“YYYYMMDDHH”的sectionIdentifier,它生成一个与第一个排序描述符“created”兼容的相对顺序。还有待解决的问题(在我看来):使用fixed with格式,并使用
hour/4
而不是
hour%4
@MartinR。解决它的人在稍早的时候意识到我使用了错误的运算符。谢谢你花时间看q!如果你愿意的话,加上这个作为答案,我会接受的。@max:法尔斯基的回答几乎是正确的。我建议他把最后的部分修好,然后你就可以接受了。我修好了接线员。可能还有其他修正,只需确保月、日和小时填充为0,这样您就可以格式化字符串,而不必进行乘法运算,这可能更有效。