- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
我正在使用 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/
我正在使用 NSFetchedResultsController 从数据库中获取数据并显示在 UITableView 中(元素根据时间戳排序)。现在我只想使用 fetchLimit 属性加载最后 20
我有一个 NSFetchedResultsController 来用核心数据中的内容更新 UITableView。这是非常标准的东西,我相信你们都看过很多次,但是我遇到了一个小问题。首先是我的代码:
我正在使用 NSFetchedResultsController 将数据填充到 UITableView 上。这是一个简单的聊天应用程序,我想首先将最新的 25 条消息加载到表格中,然后在用户向上滚动以
documentation对于 Cocoa 的 NSFetchRequest fetchLimit 属性说: The fetch limit specifies the maximum number
我目前遇到一个问题,当 FRC 的 fetchRequest 上的 fetchLimit 为 4 时,使用 NSFetchedResultsController 的 UITableViewContro
我正在使用 NSFetchResultsController 并且我正在尝试让分页工作。我在 NSFetchRequest 上遇到了这两个属性,并突出显示了我有疑问的部分: 获取限制(docs): F
我有一个选项卡式应用程序,它有 2 个选项卡和 2 个 UITableView。 我也有 2 个 NSFetchedResultsController 类型的对象,但它们都在同一个实体上,具有不同的顺
我有一个非常偶发的错误,它涉及包含 fetchLimit 和 fetchOffset 的核心数据查询。在很长一段时间内(我已经看到它发生过一次,另一个测试人员也是如此),fetchOffset 似乎被
我需要从我的 Azure 移动服务中查询一些数据。到目前为止我所知道的是,我使用 MSQuery 调用,我可以更改 fetchLimit 和 orderBy 但不能添加 systemPropertie
我在核心数据中有数千行数据。进行如此大的提取会显着降低用户体验。由于我让用户以各种方式查看数据,因此重新加载很常见,并且重新加载 FRC 可能会很痛苦。因此,我在 Fetch 中设置了 FetchLi
我是一名优秀的程序员,十分优秀!