Change to use async fetch for major account changes

This commit is contained in:
Maurice Parker 2019-08-21 15:58:55 -05:00
parent 2719772701
commit af98311c63

View File

@ -270,14 +270,14 @@ class AppCoordinator: NSObject, UndoableCommandRunner, UnreadCountProvider {
@objc func accountStateDidChange(_ note: Notification) { @objc func accountStateDidChange(_ note: Notification) {
if timelineFetcherContainsAnyPseudoFeed() { if timelineFetcherContainsAnyPseudoFeed() {
fetchAndReplaceArticlesSync() fetchAndReplaceArticlesAsync()
} }
rebuildBackingStores() rebuildBackingStores()
} }
@objc func accountsDidChange(_ note: Notification) { @objc func accountsDidChange(_ note: Notification) {
if timelineFetcherContainsAnyPseudoFeed() { if timelineFetcherContainsAnyPseudoFeed() {
fetchAndReplaceArticlesSync() fetchAndReplaceArticlesAsync()
} }
rebuildBackingStores() rebuildBackingStores()
} }
@ -916,8 +916,6 @@ private extension AppCoordinator {
} }
} }
// MARK: Fetching Articles
func queueFetchAndMergeArticles() { func queueFetchAndMergeArticles() {
fetchAndMergeArticlesQueue.add(self, #selector(fetchAndMergeArticles)) fetchAndMergeArticlesQueue.add(self, #selector(fetchAndMergeArticles))
} }
@ -946,6 +944,11 @@ private extension AppCoordinator {
} }
func cancelPendingAsyncFetches() {
fetchSerialNumber += 1
fetchRequestQueue.cancelAllRequests()
}
func fetchAndReplaceArticlesSync() { func fetchAndReplaceArticlesSync() {
// To be called when the user has made a change of selection in the sidebar. // To be called when the user has made a change of selection in the sidebar.
// It blocks the main thread, so that theres no async delay, // It blocks the main thread, so that theres no async delay,
@ -960,9 +963,17 @@ private extension AppCoordinator {
replaceArticles(with: fetchedArticles) replaceArticles(with: fetchedArticles)
} }
func cancelPendingAsyncFetches() { func fetchAndReplaceArticlesAsync() {
fetchSerialNumber += 1 // To be called when we need to do an entire fetch, but an async delay is okay.
fetchRequestQueue.cancelAllRequests() // Example: we have the Today feed selected, and the calendar day just changed.
cancelPendingAsyncFetches()
guard let timelineFetcher = timelineFetcher else {
emptyTheTimeline()
return
}
fetchUnsortedArticlesAsync(for: [timelineFetcher]) { [weak self] (articles) in
self?.replaceArticles(with: articles)
}
} }
func fetchUnsortedArticlesSync(for representedObjects: [Any]) -> Set<Article> { func fetchUnsortedArticlesSync(for representedObjects: [Any]) -> Set<Article> {