diff --git a/Frameworks/Account/CloudKit/CloudKitAccountDelegate.swift b/Frameworks/Account/CloudKit/CloudKitAccountDelegate.swift index ea7dd3770..867f6dcb0 100644 --- a/Frameworks/Account/CloudKit/CloudKitAccountDelegate.swift +++ b/Frameworks/Account/CloudKit/CloudKitAccountDelegate.swift @@ -567,8 +567,7 @@ private extension CloudKitAccountDelegate { } group.enter() - refresher.refreshFeeds(refresherWebFeeds) { refresherNewArticles, refresherUpdatedArticles, refresherDeletedArticles in - self.storeArticleChanges(new: refresherNewArticles, updated: refresherUpdatedArticles, deleted: refresherDeletedArticles) + refresher.refreshFeeds(refresherWebFeeds) { group.leave() } @@ -788,6 +787,7 @@ private extension CloudKitAccountDelegate { extension CloudKitAccountDelegate: LocalAccountRefresherDelegate { func localAccountRefresher(_ refresher: LocalAccountRefresher, didProcess articleChanges: ArticleChanges, completion: @escaping () -> Void) { + self.storeArticleChanges(new: articleChanges.newArticles, updated: articleChanges.updatedArticles, deleted: articleChanges.deletedArticles) } func localAccountRefresher(_ refresher: LocalAccountRefresher, requestCompletedFor: WebFeed) { diff --git a/Frameworks/Account/LocalAccount/LocalAccountDelegate.swift b/Frameworks/Account/LocalAccount/LocalAccountDelegate.swift index d82d3d9e6..5885c6ecc 100644 --- a/Frameworks/Account/LocalAccount/LocalAccountDelegate.swift +++ b/Frameworks/Account/LocalAccount/LocalAccountDelegate.swift @@ -52,12 +52,12 @@ final class LocalAccountDelegate: AccountDelegate { var refresherWebFeeds = Set() let webFeeds = account.flattenedWebFeeds() - + refreshProgress.addToNumberOfTasksAndRemaining(webFeeds.count) + let group = DispatchGroup() for webFeed in webFeeds { if let components = URLComponents(string: webFeed.url), let feedProvider = FeedProviderManager.shared.best(for: components) { - refreshProgress.addToNumberOfTasksAndRemaining(1) group.enter() feedProvider.refresh(webFeed) { result in switch result { @@ -77,13 +77,13 @@ final class LocalAccountDelegate: AccountDelegate { } } - refreshProgress.addToNumberOfTasksAndRemaining(refresherWebFeeds.count) group.enter() - refresher?.refreshFeeds(refresherWebFeeds) { _, _, _ in + refresher?.refreshFeeds(refresherWebFeeds) { group.leave() } group.notify(queue: DispatchQueue.main) { + self.refreshProgress.clear() account.metadata.lastArticleFetchEndTime = Date() completion(.success(())) } @@ -234,13 +234,16 @@ final class LocalAccountDelegate: AccountDelegate { } extension LocalAccountDelegate: LocalAccountRefresherDelegate { - + + func localAccountRefresher(_ refresher: LocalAccountRefresher, didProcess: ArticleChanges, completion: @escaping () -> Void) { + completion() + } + func localAccountRefresher(_ refresher: LocalAccountRefresher, requestCompletedFor: WebFeed) { refreshProgress.completeTask() } func localAccountRefresherDidFinish(_ refresher: LocalAccountRefresher) { - self.refreshProgress.clear() } } diff --git a/Frameworks/Account/LocalAccount/LocalAccountRefresher.swift b/Frameworks/Account/LocalAccount/LocalAccountRefresher.swift index f0d870430..e043e4fd1 100644 --- a/Frameworks/Account/LocalAccount/LocalAccountRefresher.swift +++ b/Frameworks/Account/LocalAccount/LocalAccountRefresher.swift @@ -14,16 +14,14 @@ import Articles import ArticlesDatabase protocol LocalAccountRefresherDelegate { + func localAccountRefresher(_ refresher: LocalAccountRefresher, didProcess: ArticleChanges, completion: @escaping () -> Void) func localAccountRefresher(_ refresher: LocalAccountRefresher, requestCompletedFor: WebFeed) func localAccountRefresherDidFinish(_ refresher: LocalAccountRefresher) } final class LocalAccountRefresher { - var newArticles = Set
() - var updatedArticles = Set
() - var deletedArticles = Set
() - private var completion: ((Set
, Set
, Set
) -> Void)? + private var completion: (() -> Void)? = nil private var isSuspended = false var delegate: LocalAccountRefresherDelegate? @@ -31,9 +29,9 @@ final class LocalAccountRefresher { return DownloadSession(delegate: self) }() - public func refreshFeeds(_ feeds: Set, completion: ((Set
, Set
, Set
) -> Void)? = nil) { + public func refreshFeeds(_ feeds: Set, completion: (() -> Void)? = nil) { guard !feeds.isEmpty else { - completion?(Set
(), Set
(), Set
()) + completion?() return } self.completion = completion @@ -105,18 +103,14 @@ extension LocalAccountRefresher: DownloadSessionDelegate { account.update(feed, with: parsedFeed) { result in if case .success(let articleChanges) = result { - - self.newArticles.formUnion(articleChanges.newArticles ?? Set
()) - self.updatedArticles.formUnion(articleChanges.updatedArticles ?? Set
()) - self.deletedArticles.formUnion(articleChanges.deletedArticles ?? Set
()) - - if let httpResponse = response as? HTTPURLResponse { - feed.conditionalGetInfo = HTTPConditionalGetInfo(urlResponse: httpResponse) + self.delegate?.localAccountRefresher(self, didProcess: articleChanges) { + if let httpResponse = response as? HTTPURLResponse { + feed.conditionalGetInfo = HTTPConditionalGetInfo(urlResponse: httpResponse) + } + feed.contentHash = dataHash + completion() + self.delegate?.localAccountRefresher(self, requestCompletedFor: feed) } - feed.contentHash = dataHash - - completion() - self.delegate?.localAccountRefresher(self, requestCompletedFor: feed) } else { completion() self.delegate?.localAccountRefresher(self, requestCompletedFor: feed) @@ -171,10 +165,8 @@ extension LocalAccountRefresher: DownloadSessionDelegate { } func downloadSessionDidCompleteDownloadObjects(_ downloadSession: DownloadSession) { - completion?(newArticles, updatedArticles, deletedArticles) + completion?() completion = nil - newArticles = Set
() - deletedArticles = Set
() delegate?.localAccountRefresherDidFinish(self) }