每4小时将NSFetchedResultsController组分成多个部分
我试图将一系列NSManagedObjects分组。 每节将代表一天内的4小时。
- (NSString *)sectionIdentifier
{
// Create and cache the section identifier on demand.
[self willAccessValueForKey:@"sectionIdentifier"];
NSString *tmp = [self primitiveSectionIdentifier];
[self didAccessValueForKey:@"sectionIdentifier"];
if (!tmp)
{
NSCalendar *calendar = [NSCalendar currentCalendar];
NSDateComponents *components = [calendar components:(NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit | NSHourCalendarUnit) fromDate:[self created]];
tmp = [NSString stringWithFormat:@"%.0f %d %d %d", round(components.hour % 4), components.day, components.month, components.year];
[self setPrimitiveSectionIdentifier:tmp];
}
return tmp;
}
在NSManagedObject中找到的上述代码中,transient属性sectionIdentifier
返回一个字符串,它包含它所处的时间段(4小时),其创建的日期,月份和年份。
但是,尽管NSFetchedResultController的fetchRequest的排序描述符被设置为按照创建日期的顺序对结果进行排序(最新的对象位于最底层,最顶层的最旧对象),但由于节标识符未被正确排序,所以获取请求失败。
- (NSFetchedResultsController *) fetchedResultsController {
if (!_fetchedResultsController) {
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
[fetchRequest setEntity:[NSEntityDescription entityForName:@"PSSpace" inManagedObjectContext:self.managedObjectContext]];
[fetchRequest setSortDescriptors:@[[NSSortDescriptor sortDescriptorWithKey:@"created" ascending:YES]]];
_fetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:self.managedObjectContext sectionNameKeyPath:@"sectionIdentifier" cacheName:nil];
[_fetchedResultsController setDelegate:self];
}
return _fetchedResultsController;
}
CoreData:error:(NSFetchedResultsController)索引为10的提取对象的乱序部分名称为'1 14 12 2013.对象必须按部分名称'
请你能告诉我怎样才能解决这个问题的顺序?
谢谢。
尝试类似
if (!tmp) {
NSCalendar *calendar = [NSCalendar currentCalendar];
NSDateComponents *components = [calendar components:(NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit | NSHourCalendarUnit) fromDate:[self created]];
NSUInteger _y = (components.year * 1000000);
NSUInteger _m = (components.month * 10000);
NSUInteger _d = (components.day * 100);
NSUInteger _h = round(components.hour / 4);
NSUInteger token = (_y + _m + _d + _h);
tmp = [NSString stringWithFormat:@"%lu", token];
[self setPrimitiveSectionIdentifier:tmp];
}
链接地址: http://www.djcxy.com/p/36115.html
上一篇: NSFetchedResultsController group into sections for every 4 hours