gpt4 book ai didi

ios - 如何按相反方向对获取的请求结果进行排序?

转载 作者:行者123 更新时间:2023-11-29 13:20:08 25 4
gpt4 key购买 nike

我有实体消息。每条消息都包含时间戳。我只想一次显示 20 条最后的消息,从最旧的到最新的。

要获得最后 20 个,我执行以下代码

NSEntityDescription *entity = [NSEntityDescription entityForName:@"Message" inManagedObjectContext:self.managedObjectContext];
[fetchRequest setEntity:entity];
[fetchRequest setFetchBatchSize:20];
[fetchRequest setFetchLimit:20];
NSSortDescriptor *timeDescriptor = [[[NSSortDescriptor alloc] initWithKey:@"time" ascending:NO] autorelease];
[fetchRequest setSortDescriptors:timeDescriptor];
NSFetchedResultsController *aFetchedResultsController = [[[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:self.managedObjectContext sectionNameKeyPath:@"data" cacheName:nil] autorelease];
aFetchedResultsController.delegate = self;
self.fetchedResultsController = aFetchedResultsController;

但在结果中我想显示升序:是。有什么办法吗?

最佳答案

您不能只取总数 # 并减去当前行以获得反向排序。所以 0 -> 20、1 -> 19、... 20 -> 0。假设您使用的是带有 1 个部分的 tableView,代码看起来像(未经测试):

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{

id <NSFetchedResultsSectionInfo> sectionInfo = [[self.fetchedResultsController sections] objectAtIndex:0];
NSInteger reverseRow = [sectionInfo numberOfObjects] - indexPath.row - 1;
NSIndexPath *reverseIndexPath = [NSIndexPath indexPathForRow:reverseRow inSection:indexPath.section];
id myObject = [self.fetchedResultsController objectAtIndexPath:reverseIndexPath];
}

关于ios - 如何按相反方向对获取的请求结果进行排序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14568542/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com