- r - 以节省内存的方式增长 data.frame
- ruby-on-rails - ruby/ruby on rails 内存泄漏检测
- android - 无法解析导入android.support.v7.app
- UNIX 域套接字与共享内存(映射文件)
我有一个 NSFetchedResultsController 来用核心数据中的内容更新 UITableView。这是非常标准的东西,我相信你们都看过很多次,但是我遇到了一个小问题。首先是我的代码:
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Article" inManagedObjectContext:self.managedObjectContext];
[fetchRequest setEntity:entity];
[fetchRequest setFetchLimit:20];
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"(folder.hidden == NO)"];
[fetchRequest setPredicate:predicate];
NSSortDescriptor *sort1 = [NSSortDescriptor sortDescriptorWithKey:@"sortDate" ascending:NO];
[fetchRequest setSortDescriptors:[NSArray arrayWithObjects:sort1, nil]];
NSFetchedResultsController *controller = [[NSFetchedResultsController alloc]
initWithFetchRequest:fetchRequest
managedObjectContext:self.managedObjectContext
sectionNameKeyPath:nil
cacheName:nil];
[fetchRequest release];
controller.delegate = self;
self.fetchedResultsController = controller;
[controller release];
NSError *error = nil;
[self.fetchedResultsController performFetch:&error];
if (error) {
// TODO send error notification
NSLog(@"%@", [error localizedDescription]);
}
问题是最初商店没有实体,因为它从网络服务下载和同步。发生的情况是 NSFetchedResultsController 用商店中超过 150 行的实体填充表,这是 web 服务返回的数量。但我将提取限制设置为 20,它似乎被忽略了。但是,如果我关闭该应用程序并使用商店中已有的数据重新开始,它就可以正常工作。我是我的代表,我这样做:
#pragma mark -
#pragma mark NSFetchedResultsControllerDelegate methods
- (void)controllerWillChangeContent:(NSFetchedResultsController *)controller {
[self.tableView beginUpdates];
}
- (void)controller:(NSFetchedResultsController *)controller didChangeSection:(id <NSFetchedResultsSectionInfo>)sectionInfo
atIndex:(NSUInteger)sectionIndex forChangeType:(NSFetchedResultsChangeType)type {
switch(type) {
case NSFetchedResultsChangeInsert:
[self.tableView insertSections:[NSIndexSet indexSetWithIndex:sectionIndex] withRowAnimation:UITableViewRowAnimationFade];
break;
case NSFetchedResultsChangeDelete:
[self.tableView deleteSections:[NSIndexSet indexSetWithIndex:sectionIndex] withRowAnimation:UITableViewRowAnimationFade];
break;
}
}
- (void)controller:(NSFetchedResultsController *)controller didChangeObject:(id)anObject
atIndexPath:(NSIndexPath *)indexPath forChangeType:(NSFetchedResultsChangeType)type newIndexPath:(NSIndexPath *)newIndexPath {
UITableView *tableView = self.tableView;
switch(type) {
case NSFetchedResultsChangeInsert:
[tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:newIndexPath] withRowAnimation:UITableViewRowAnimationFade];
break;
case NSFetchedResultsChangeDelete:
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
break;
case NSFetchedResultsChangeUpdate:
[self configureCell:[tableView cellForRowAtIndexPath:indexPath] atIndexPath:indexPath];
break;
case NSFetchedResultsChangeMove:
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
[tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:newIndexPath] withRowAnimation:UITableViewRowAnimationFade];
break;
}
}
- (void)controllerDidChangeContent:(NSFetchedResultsController *)controller {
[self.tableView endUpdates];
}
这几乎是从 Apple 的开发文档中复制粘贴的,知道发生了什么事吗?
最佳答案
我知道这是一个老问题,但我有一个解决方案:
由于 NSFetchedResultsController
中存在一个已知错误,它不遵守 NSFetchRequest
的 fetchlimit
,因此您必须手动处理限制UITableViewDataSource
和 NSFetchedResultsControllerDelegate
方法中的记录数。
tableView:numberOfRowsInSection:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
id <NSFetchedResultsSectionInfo> sectionInfo = [[self.fetchedResultsController sections] objectAtIndex:section];
NSInteger numRows = [sectionInfo numberOfObjects];
if (numRows > self.fetchedResultsController.fetchRequest.fetchLimit) {
numRows = self.fetchedResultsController.fetchRequest.fetchLimit;
}
return numRows;
}
Controller :didChangeObject:atIndexPath:forChangeType:newIndexPath:
- (void)controller:(NSFetchedResultsController *)controller didChangeObject:(id)anObject atIndexPath:(NSIndexPath *)indexPath forChangeType:(NSFetchedResultsChangeType)type newIndexPath:(NSIndexPath *)newIndexPath {
switch(type) {
case NSFetchedResultsChangeInsert:
if ([self.tableView numberOfRowsInSection:0] == self.fetchedResultsController.fetchRequest.fetchLimit) {
//Determining which row to delete depends on your sort descriptors
[self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:[NSIndexPath indexPathForRow:self.fetchedResultsController.fetchRequest.fetchLimit - 1 inSection:0]] withRowAnimation:UITableViewRowAnimationFade];
}
[self.tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:newIndexPath]
withRowAnimation:UITableViewRowAnimationFade];
break;
...
}
}
关于iphone - NSFetchedResultsController 忽略 fetchLimit?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4858228/
我正在尝试使用具有混合语言数据的 FRC,并希望有一个部分索引。 从文档看来您应该能够覆盖 FRC - (NSString *)sectionIndexTitleForSectionName:(NSS
使用等效的 NSFetchRequest 重新创建 NSFetchedResultsController 时,为什么 [NSFetchedResultsController PerformFetch:
我正在尝试起草一个可重复使用的类,该类基本上可重复用于具有几乎相同实现的多个实体。基本上我将有几个 UIViewController,每个都包含 UIView,其中包含 UITableView 以及由
我有一个 iPhone 和一个 Android 应用程序。在 iPhone 上,我使用 NSFetchedResultsController 来管理我的 UITableView 的数据。目前我有很多数
NSFetchedResultsController 监视对正在跟踪的整个托管对象的更改。每当在当前上下文中修改任何属性时,例如 – controller:didChangeObject:atInde
自 1 个月以来,我一直在使用 NSFetchedResultsController 构建应用程序,并在 3.1.2 SDK 上测试该应用程序。问题是我一直在我的应用程序中到处使用 NSFetched
我有一个人物和宠物的数据库,具有一对多关系 人宠物 姓名 姓名 宠物>主人 我正在使用由核心数据支持的 UITableView 和 nsfetchedresultscontroller 来显示宠物列表
我有一个 NSFetchedResultsController,它在 TableView 中显示项目列表,包括关联实体的计数。当为此关联添加对象时(使用 addXXXObject),不会调用回调来通知
花了几个小时后,我发现无法在以下文章中由 SQLite 支持的 NSFetchedResultsController 中进行自定义排序。 NSFetchedResultsController cust
我设置了一个 NSFetchedResultsController 来填充 UITableView,基于我的“主”NSManagedObjectContext。 在计时器中,我不断地将对象添加到单独的
在我的应用程序中,我需要循环遍历核心数据中的所有实体,并且我正在使用 NSFetchedresultcontroller。 我现在正在这样做: NSArray *tempArray = [[NSArr
我正在开发我的第一个核心数据支持的应用程序,但无法弄清楚如何正确设置 NSFetchedResultsController。我有两个实体: /-----------\ /--------
我正在使用 NSFetchedResultsController 在 TableView 中显示项目: - (NSInteger)numberOfSectionsInTableView:(UITabl
我将 NSFetchedResultsController 放入我的代码中,这样我就可以对 TableView 数据进行很好的自动分段。 所以我正在运行测试以确保一切正常。我的持久存储中有一个 Boo
我如何使用带有翻译后的排序键和sectionKeyPath的NSFetchedResultsController? 问题:我在数据库中的属性“type”中有 ID,例如 typeA、typeB、typ
大家好 我的 CoreData 驱动应用程序有多个来自同一存储的数据 View ,并且全部使用 NSFetchedResultsController。目前,它们的 NSFetchedResultsCo
我想使用使用 bool 值设置的不同谓词从 NSFetchedResultsController 重新获取数据。如何刷新 NSFetchedResultsController 以获取一组新数据? -
核心数据在 NSManagedObjectContext 类中提供了“executeFetchRequest”方法,我们可以使用该方法从表中获取数据并以任何需要的方式使用它。 现在还有另一种方法,即使
我正在编写一个应用程序,一个屏幕上有两个表格。左表是文件夹列表,右表显示文件列表。当点击左侧的一行时,右侧表格将显示属于该文件夹的文件。 我正在使用 Core Data 进行存储。当文件夹的选择发生变
我有一个由 NSFetchedResultsController 管理的 TableView 。但是,我在查找或创建操作时遇到问题。当用户点击表格 View 的底部时,我正在向服务器查询另一批内容。如
我是一名优秀的程序员,十分优秀!