为NSFetchedResultsControllerdynamic设置fetchLimit
我正在使用NSFetchedResultsController将数据填充到UITableView。 这是一个简单的聊天应用程序,我想先加载最新的25条消息到桌子上,并加载更多的用户滚动查看旧信息(聊天信息是在升序)。
我调用setFetchLimit方法:对于willDisplayCell中的NSFetchedResultsController:像这样…
- (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;
回到问题。
如何dynamic地改变NSFetchedResultsController的fetchLimit?
任何命中将是真棒! 谢谢!
想出了这一个。 看起来像我必须运行performFetch:更改fetchLimit后。 :d
[self.resultsController.fetchRequest setFetchLimit:maxListItems]; [self.resultsController performFetch:&error];
Instand使用setFetchLimit
,使用setBatchSize
,详见下面的答案。
fetchedObjects数组的计数可能不是您想要执行的操作,因为它不会更新持久性存储中的更改。 从NSFetchedResultsController文档:
结果数组只包含由获取请求(fetchRequest)指定的实体的实例并且与其谓词相匹配。 (如果获取请求没有谓词,则结果数组包含由获取请求指定的实体的所有实例。)
结果数组反映了控制器托pipe对象上下文中托pipe对象的内存中状态,而不是持久性存储中的状态。 但是,返回的数组不会随着托pipe对象的插入,修改或删除而更新。
如果您只想获取20个对象,请设置NSFetchRequest的获取限制。 如果只想在内存中保留20个对象,请使用NSFetchRequest对象的setBatchSize。
- 如何拒绝从新的Itunes界面提交的二进制文件
- iOS 7.1幻灯片解锁文本animation
- iOS – 自定义UISearchBar的取消button
- 如何pipe理在iOS上的MKAnnotationView的拖放?
- 如果我的已发行v1版本没有版本化的核心数据模型,我可以使用“自动轻量级迁移”吗?
- 如何使用NSDateFormatter将NSString转换为NSDate?
- 将textLabel垂直放置在tableView的UITableViewHeaderFooterView中:willDisplayHeaderView:forSection:
- 如何一个应用程序知道什么其他应用程序已经安装在设备中
- 儿童用应用程式正在追踪他们