gpt4 book ai didi

iphone - 为 NSFetchedResultsController 动态设置 fetchLimit

转载 作者:可可西里 更新时间:2023-11-01 03:38:24 25 4
gpt4 key购买 nike

我正在使用 NSFetchedResultsController 将数据填充到 UITableView 上。这是一个简单的聊天应用程序,我想首先将最新的 25 条消息加载到表格中,然后在用户向上滚动以查看较旧的消息时加载更多消息(聊天消息按升序排列)。

我为 willDisplayCell: 中的 NSFetchedResultsController 调用了一个方法 setFetchLimit: 就像这样......

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
if(indexPath.row == 0)
{
[self performSelector:@selector(getMoreMessages) withObject:nil afterDelay:1.0];
}
}

当显示 UITableView 的第一行时,getMoreMessages 将尝试重置 fetchLimit 重新加载 UITableView,如下所示......

- (void)getMoreMessages
{
maxListItems += 25;
NSLog(@"set maxListItems: %d", maxListItems);
[self.resultsController.fetchRequest setFetchLimit:maxListItems];
[self._tableView reloadData];
}

不过好像不行,表数据不会变。最初的 NSFetchRequest 是这样设置的...

NSFetchRequest *chatDataRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"ChatData" inManagedObjectContext:appDelegate.managedObjectContext];
[chatDataRequest setEntity:entity];

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"(key != 0 OR messageNo != 0) and matchNo = %d", matchNo];
[chatDataRequest setPredicate:predicate];

NSSortDescriptor *sortDescripter1 = [[NSSortDescriptor alloc] initWithKey:@"status" ascending:YES];
NSSortDescriptor *sortDescripter2 = [[NSSortDescriptor alloc] initWithKey:@"messageNo" ascending:YES];
NSArray *sortDescripters = [[NSArray alloc] initWithObjects:sortDescripter1, sortDescripter2, nil];
[chatDataRequest setSortDescriptors:sortDescripters];
[sortDescripters release];
[sortDescripter1 release];
[sortDescripter2 release];

[chatDataRequest setFetchLimit:25];

NSFetchedResultsController *fetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:chatDataRequest managedObjectContext:appDelegate.managedObjectContext sectionNameKeyPath:nil cacheName:[NSString stringWithFormat:@"%d_chat.cache", matchNumber]];
[chatDataRequest release];
fetchedResultsController.delegate = self;
NSError *error;
BOOL success = [fetchedResultsController performFetch:&error];
if(!success) NSLog(@"error: %@", error);
self.resultsController = fetchedResultsController;

回到问题。

如何动态更改 NSFetchedResultsController 的 fetchLimit?

任何命中都会很棒!谢谢!

最佳答案

使用setFetchLimit,使用setBatchSize,详见下文。

fetchedObjects 数组的计数可能不是您想要的,因为它不会更新持久存储中的更改。来自 NSFetchedResultsController 文档:

The results array only includes instances of the entity specified by the fetch request (fetchRequest) and that match its predicate. (If the fetch request has no predicate, then the results array includes all instances of the entity specified by the fetch request.)

The results array reflects the in-memory state of managed objects in the controller’s managed object context, not their state in the persistent store. The returned array does not, however, update as managed objects are inserted, modified, or deleted.

如果您只想获取 20 个对象,请设置 NSFetchRequest 的获取限制。如果只想在内存中保留 20 个对象,请使用 NSFetchRequest 对象的 setBatchSize。

关于iphone - 为 NSFetchedResultsController 动态设置 fetchLimit,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6756148/

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