![](/img/trans.png)
[英]NSFetchedResultsController Not Updating Upon Return from Modal View Controller
[英]How to prevent NSFetchedResultsController from updating tableview when the controller disappears?
我有一個UITabbar
, UITabbar
有多個控制器。 其中一個控制器用於向Core Data添加事件,而另一個控制器用於使用NSFetchedResultsController
在UITableView中顯示事件。
這是我想要實現的行為:在消失時,UITableView停止更新,當用戶返回時,重新加載整個表視圖。 否則,從其他控制器插入事件需要更長的時間,因為在UITableView
中創建了新行,甚至認為它不可見。
我想知道如何實現這種行為,因為它似乎沒有像我預期的那樣工作:
我在viewWillDisappear
中將NSFetchedResultsController的委托設置為nil,並在viewWillAppear
恢復它,同時調用[UITableView reloadData]
;
不知何故,我沒有看到新數據,並懷疑這是由於NSFetchedResultsController如果沒有委托就停止提取的方式。
當UITableView
消失時,如何正確“暫停” UITableView
更新,但是當控制器重新出現時,仍然可以看到整個數據集?
嘗試發送performFetch:
到NSFetchedResultsController
在viewWillAppear:
您設置其委托回來后,以self
。
我認為你不必“暫停”表視圖更新。 無論如何, UITableView
只會從NSFetchedResultsController
請求可見單元格的數據。 如果表視圖不可見,則不會觸發更新。
您是否測試過從其他控制器插入事件是否需要更長時間? 我對此表示懷疑。 樂器說什么?
如果您的委托方法被觸發,您仍然可以在進行任何更新之前檢查表視圖是否可見。
在那之后,你完全按照rob的建議去做performFetch:
在viewWillAppear:
執行performFetch:
viewWillAppear:
.
不要在viewWillDisappear
中將NSFetchedResultsController
的delegate
設置為nil,而是嘗試將NSFetchedResultsController
的對象設置為nil
這種原油方法怎么樣? 沒測試過。
@property (nonatomic) BOOL bruteForceReload;
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
self.bruteForceReload = NO;
}
-(void)viewDidDisappear:(BOOL)animated {
[super viewDidDisappear:animated];
self.bruteForceReload = YES;
}
-(void)setBruteForceReload:(BOOL)bruteForceReload {
_bruteForceReload = bruteForceReload;
if (_bruteForceReload) {
[self.tableView reloadData];
}
}
- (void)controllerWillChangeContent:(NSFetchedResultsController *)controller
{
if (!self.bruteForceReload) {
[self.tableView beginUpdates];
}
}
- (void)controller:(NSFetchedResultsController *)controller didChangeSection:(id <NSFetchedResultsSectionInfo>)sectionInfo
atIndex:(NSUInteger)sectionIndex forChangeType:(NSFetchedResultsChangeType)type
{
if (!self.bruteForceReload) {
switch(type) {
case NSFetchedResultsChangeInsert:
[self.tableView insertSections:[NSIndexSet indexSetWithIndex:sectionIndex] withRowAnimation:UITableViewRowAnimationFade];
break;
case NSFetchedResultsChangeDelete:
[self.tableView deleteSections:[NSIndexSet indexSetWithIndex:sectionIndex] withRowAnimation:UITableViewRowAnimationFade];
break;
default:
return;
}
}
}
- (void)controller:(NSFetchedResultsController *)controller didChangeObject:(id)anObject
atIndexPath:(NSIndexPath *)indexPath forChangeType:(NSFetchedResultsChangeType)type
newIndexPath:(NSIndexPath *)newIndexPath
{
if (!self.bruteForceReload) {
UITableView *tableView = self.tableView;
switch(type) {
case NSFetchedResultsChangeInsert:
[tableView insertRowsAtIndexPaths:@[newIndexPath] withRowAnimation:UITableViewRowAnimationFade];
break;
case NSFetchedResultsChangeDelete:
[tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
break;
case NSFetchedResultsChangeUpdate:
[self configureCell:[tableView cellForRowAtIndexPath:indexPath] atIndexPath:indexPath];
break;
case NSFetchedResultsChangeMove:
[tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
[tableView insertRowsAtIndexPaths:@[newIndexPath] withRowAnimation:UITableViewRowAnimationFade];
break;
}
}
}
- (void)controllerDidChangeContent:(NSFetchedResultsController *)controller
{
if (!self.bruteForceReload) {
[self.tableView endUpdates];
} else {
[self.tableView reloadData];
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.