gpt4 book ai didi

iphone - NSFetchedResultsController 忽略 fetchLimit?

转载 作者:IT王子 更新时间:2023-10-29 07:59:29 25 4
gpt4 key购买 nike

我有一个 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 中存在一个已知错误,它不遵守 NSFetchRequestfetchlimit,因此您必须手动处理限制UITableViewDataSourceNSFetchedResultsControllerDelegate 方法中的记录数。

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/

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