diff --git a/WordPress/Classes/Utility/WPTableViewHandler.h b/WordPress/Classes/Utility/WPTableViewHandler.h index cf8bce7ad2f1..f86fe1f800eb 100644 --- a/WordPress/Classes/Utility/WPTableViewHandler.h +++ b/WordPress/Classes/Utility/WPTableViewHandler.h @@ -111,6 +111,7 @@ - (void)clearCachedRowHeights; - (void)refreshCachedRowHeightsForWidth:(CGFloat)width; - (void)invalidateCachedRowHeightAtIndexPath:(nonnull NSIndexPath *)indexPath; +- (void)resetResultsController; /** A convenience method for clearing cached row heights and reloading the table view. diff --git a/WordPress/Classes/Utility/WPTableViewHandler.m b/WordPress/Classes/Utility/WPTableViewHandler.m index fb05adf033b4..6bfa196e777b 100644 --- a/WordPress/Classes/Utility/WPTableViewHandler.m +++ b/WordPress/Classes/Utility/WPTableViewHandler.m @@ -286,6 +286,11 @@ - (void)invalidateCachedRowHeightAtIndexPath:(NSIndexPath *)indexPath [self clearCachedRowHeightAtIndexPath:indexPath]; } +- (void)resetResultsController +{ + _resultsController = nil; +} + #pragma mark - Required Delegate Methods @@ -598,10 +603,6 @@ - (void)scrollViewWillEndDragging:(UIScrollView *)scrollView withVelocity:(CGPoi - (NSFetchedResultsController *)resultsController { - if (_resultsController != nil && ![_resultsController.fetchRequest.entityName isEqualToString:[self fetchRequest].entityName]) { - _resultsController = nil; - } - if (_resultsController != nil) { return _resultsController; } diff --git a/WordPress/Classes/ViewRelated/Reader/ReaderStreamViewController.swift b/WordPress/Classes/ViewRelated/Reader/ReaderStreamViewController.swift index 25b2b24675ed..0c288772587b 100644 --- a/WordPress/Classes/ViewRelated/Reader/ReaderStreamViewController.swift +++ b/WordPress/Classes/ViewRelated/Reader/ReaderStreamViewController.swift @@ -2112,6 +2112,7 @@ extension ReaderStreamViewController: ReaderContentViewController { isContentFiltered = content.topicType == .tag || content.topicType == .site readerTopic = content.topicType == .discover ? nil : content.topic contentType = content.type + self.content.resetResultsController() guard !shouldDisplayNoTopicController else { return diff --git a/WordPress/Classes/ViewRelated/Reader/ReaderTableContent.swift b/WordPress/Classes/ViewRelated/Reader/ReaderTableContent.swift index 9dfd92bde5b5..4324c6361c8c 100644 --- a/WordPress/Classes/ViewRelated/Reader/ReaderTableContent.swift +++ b/WordPress/Classes/ViewRelated/Reader/ReaderTableContent.swift @@ -9,6 +9,16 @@ final class ReaderTableContent { tableViewHandler?.delegate = delegate } + func resetResultsController() { + tableViewHandler?.resetResultsController() + tableViewHandler?.tableView.reloadData() + tableViewHandler?.tableView.layoutIfNeeded() + + if !isEmpty { + tableViewHandler?.tableView.scrollToRow(at: IndexPath(row: 0, section: 0), at: .top, animated: false) + } + } + /// The fetch request can need a different predicate depending on how the content /// being displayed has changed (blocking sites for instance). Call this method to /// update the fetch request predicate and then perform a new fetch. @@ -16,6 +26,7 @@ final class ReaderTableContent { func updateAndPerformFetchRequest(predicate: NSPredicate) { assert(Thread.isMainThread, "Reader Error: updating fetch request on a background thread.") + tableViewHandler?.resetResultsController() tableViewHandler?.resultsController?.fetchRequest.predicate = predicate do { try tableViewHandler?.resultsController?.performFetch()