From aba0d15cb6173346a4cba68f56395ae18b4dd182 Mon Sep 17 00:00:00 2001 From: Maurice Parker Date: Wed, 2 Oct 2019 16:41:32 -0500 Subject: [PATCH] Modify background fetch so that it doesn't have to use a background process --- Frameworks/Account/AccountManager.swift | 11 +++- .../LocalAccount/LocalAccountDelegate.swift | 6 +- .../LocalAccount/LocalAccountRefresher.swift | 11 +++- iOS/AppDelegate.swift | 66 ++----------------- submodules/RSWeb | 2 +- 5 files changed, 30 insertions(+), 66 deletions(-) diff --git a/Frameworks/Account/AccountManager.swift b/Frameworks/Account/AccountManager.swift index 7db38dc48..b6eee62d8 100644 --- a/Frameworks/Account/AccountManager.swift +++ b/Frameworks/Account/AccountManager.swift @@ -156,9 +156,13 @@ public final class AccountManager: UnreadCountProvider { return accountsDictionary[accountID] } - public func refreshAll(errorHandler: @escaping (Error) -> Void) { + public func refreshAll(errorHandler: @escaping (Error) -> Void, completion: (() ->Void)? = nil) { + let group = DispatchGroup() + activeAccounts.forEach { account in + group.enter() account.refreshAll() { result in + group.leave() switch result { case .success: break @@ -167,6 +171,11 @@ public final class AccountManager: UnreadCountProvider { } } } + + group.notify(queue: DispatchQueue.main) { + completion?() + } + } public func syncArticleStatusAll(completion: (() -> Void)? = nil) { diff --git a/Frameworks/Account/LocalAccount/LocalAccountDelegate.swift b/Frameworks/Account/LocalAccount/LocalAccountDelegate.swift index 5ebd94551..97cc2e2ec 100644 --- a/Frameworks/Account/LocalAccount/LocalAccountDelegate.swift +++ b/Frameworks/Account/LocalAccount/LocalAccountDelegate.swift @@ -31,10 +31,10 @@ final class LocalAccountDelegate: AccountDelegate { return refresher.progress } - // LocalAccountDelegate doesn't wait for completion before calling the completion block func refreshAll(for account: Account, completion: @escaping (Result) -> Void) { - refresher.refreshFeeds(account.flattenedFeeds()) - completion(.success(())) + refresher.refreshFeeds(account.flattenedFeeds()) { + completion(.success(())) + } } func sendArticleStatus(for account: Account, completion: @escaping (() -> Void)) { diff --git a/Frameworks/Account/LocalAccount/LocalAccountRefresher.swift b/Frameworks/Account/LocalAccount/LocalAccountRefresher.swift index 90718a233..de5e08f2c 100644 --- a/Frameworks/Account/LocalAccount/LocalAccountRefresher.swift +++ b/Frameworks/Account/LocalAccount/LocalAccountRefresher.swift @@ -14,6 +14,8 @@ import Articles final class LocalAccountRefresher { + private var completion: (() -> Void)? + private lazy var downloadSession: DownloadSession = { return DownloadSession(delegate: self) }() @@ -22,7 +24,8 @@ final class LocalAccountRefresher { return downloadSession.progress } - public func refreshFeeds(_ feeds: Set) { + public func refreshFeeds(_ feeds: Set, completion: @escaping () -> Void) { + self.completion = completion downloadSession.downloadObjects(feeds as NSSet) } } @@ -102,6 +105,12 @@ extension LocalAccountRefresher: DownloadSessionDelegate { func downloadSession(_ downloadSession: DownloadSession, didReceiveNotModifiedResponse: URLResponse, representedObject: AnyObject) { } + + func downloadSessionDidCompleteDownloadObjects(_ downloadSession: DownloadSession) { + completion?() + completion = nil + } + } // MARK: - Utility diff --git a/iOS/AppDelegate.swift b/iOS/AppDelegate.swift index b08a84ce0..b3141d7ca 100644 --- a/iOS/AppDelegate.swift +++ b/iOS/AppDelegate.swift @@ -265,44 +265,16 @@ private extension AppDelegate { scheduleBackgroundFeedRefresh() // schedule next refresh - var startingUnreadCount = 0 - - DispatchQueue.global(qos: .background).async { [unowned self] in - - os_log("Woken to perform account refresh.", log: self.log, type: .info) - - os_log("Getting unread count.", log: self.log, type: .info) - while(!AccountManager.shared.isUnreadCountsInitialized) { - os_log("Waiting for unread counts to be initialized...", log: self.log, type: .info) - sleep(1) - } - os_log(.info, log: self.log, "Got unread count: %i", self.unreadCount) - startingUnreadCount = self.unreadCount - - DispatchQueue.main.async { - AccountManager.shared.refreshAll(errorHandler: ErrorHandler.log) - } - os_log("Accounts requested to begin refresh.", log: self.log, type: .info) - - sleep(1) - while (!AccountManager.shared.combinedRefreshProgress.isComplete) { - os_log("Waiting for account refresh processing to complete...", log: self.log, type: .info) - sleep(1) - } - - if startingUnreadCount < self.unreadCount { - os_log("Updating unread count badge, posting notification.", log: self.log, type: .info) - self.sendReceivedArticlesUserNotification(newArticleCount: self.unreadCount - startingUnreadCount) - task.setTaskCompleted(success: true) - } else { + os_log("Woken to perform account refresh.", log: self.log, type: .info) + + DispatchQueue.main.async { + AccountManager.shared.refreshAll(errorHandler: ErrorHandler.log) { + AccountManager.shared.saveAll() os_log("Account refresh operation completed.", log: self.log, type: .info) task.setTaskCompleted(success: true) } - - AccountManager.shared.saveAll() - } - + // set expiration handler task.expirationHandler = { os_log("Accounts refresh processing terminated for running too long.", log: self.log, type: .info) @@ -311,29 +283,3 @@ private extension AppDelegate { } } - -private extension AppDelegate { - - func sendReceivedArticlesUserNotification(newArticleCount: Int) { - - let content = UNMutableNotificationContent() - content.title = NSLocalizedString("Article Download", comment: "New Articles") - - let body: String = { - if newArticleCount == 1 { - return NSLocalizedString("You have downloaded 1 new article.", comment: "Article Downloaded") - } else { - let formatString = NSLocalizedString("You have downloaded %d new articles.", comment: "Articles Downloaded") - return NSString.localizedStringWithFormat(formatString as NSString, newArticleCount) as String - } - }() - - content.body = body - content.sound = UNNotificationSound.default - - let request = UNNotificationRequest.init(identifier: "NewArticlesReceived", content: content, trigger: nil) - UNUserNotificationCenter.current().add(request) - - } - -} diff --git a/submodules/RSWeb b/submodules/RSWeb index 9cb7ca961..9d5a76e50 160000 --- a/submodules/RSWeb +++ b/submodules/RSWeb @@ -1 +1 @@ -Subproject commit 9cb7ca96182b3320882522708a2b4dcdaafb07f6 +Subproject commit 9d5a76e50d74643b331169dbd10b170c585ca979