Modify background fetch so that it doesn't have to use a background process
This commit is contained in:
parent
281416eaee
commit
aba0d15cb6
|
@ -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) {
|
||||
|
|
|
@ -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, Error>) -> Void) {
|
||||
refresher.refreshFeeds(account.flattenedFeeds())
|
||||
completion(.success(()))
|
||||
refresher.refreshFeeds(account.flattenedFeeds()) {
|
||||
completion(.success(()))
|
||||
}
|
||||
}
|
||||
|
||||
func sendArticleStatus(for account: Account, completion: @escaping (() -> Void)) {
|
||||
|
|
|
@ -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<Feed>) {
|
||||
public func refreshFeeds(_ feeds: Set<Feed>, 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
|
||||
|
|
|
@ -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)
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -1 +1 @@
|
|||
Subproject commit 9cb7ca96182b3320882522708a2b4dcdaafb07f6
|
||||
Subproject commit 9d5a76e50d74643b331169dbd10b170c585ca979
|
Loading…
Reference in New Issue