NSFetchedResultsController忽略fetchLimit? [英] NSFetchedResultsController ignores fetchLimit?
问题描述
我有一个NSFetchedResultsController来更新一个UITableView与Core Data的内容。这是很标准的东西,我相信你已经看到很多次,但我遇到了轻微的问题。首先是我的代码:
I have a NSFetchedResultsController to update a UITableView with content from Core Data. It's pretty standard stuff I'm sure you've all seen many times however I am running into slight problem. First here's my code:
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]);
}
问题是,最初商店没有实体,因为它下载和同步web服务。会发生什么,NSFetchedResultsController从存储中填充超过150行的实体,这是webservice返回的数量。但我设置一个获取限制为20,它似乎是忽略。但是,如果我关闭了应用程序,并重新开始数据已经在商店,它的工作正常。我的代表我这样做:
The problem is that initially the store has no entities as it downloads and syncs from a webservice. What happens is that the NSFetchedResultsController fills the table with over 150 rows of entities from the store, which is how many the webservice returns. But I am setting a fetch limit of 20 which it appears to be ignoring. However, if I close out the app and start again with data already in the store, it works fine. Im my delegate i do this:
#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];
}
这几乎是从苹果的开发文档复制粘贴,任何想法什么是goin
Which is pretty much copy-paste from Apple's dev documents, any ideas what's goin on?
推荐答案
我知道这是一个老问题,但我有一个解决方案:
Since there is a known bug in NSFetchedResultsController
that doesn't honor the fetchlimit
of the NSFetchRequest
, you have to manually handle the limiting of records within your UITableViewDataSource
and NSFetchedResultsControllerDelegate
methods.
由于 NSFetchedResultsController
中有一个已知的错误,因此不能满足 fetchlimit
NSFetchRequest
,您必须手动处理您的 UITableViewDataSource
和 NSFetchedResultsControllerDelegate
方法。
tableView:numberOfRowsInSection:
tableView:numberOfRowsInSection: p> - (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;
...
}
}
这篇关于NSFetchedResultsController忽略fetchLimit?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!