Implement background downloading of feeds

This commit is contained in:
Maurice Parker 2019-04-24 07:30:35 -05:00
parent 84b8b04552
commit f4c0fca6a8
3 changed files with 34 additions and 44 deletions

View File

@ -105,23 +105,6 @@ extension LocalAccountRefresher: DownloadSessionDelegate {
return true
}
func downloadSession(_ downloadSession: DownloadSession, didReceiveUnexpectedResponse response: URLResponse, representedObject: AnyObject) {
// guard let feed = representedObject as? Feed else {
// return
// }
//
// print("Unexpected response \(response) for \(feed.url).")
}
func downloadSession(_ downloadSession: DownloadSession, didReceiveNotModifiedResponse: URLResponse, representedObject: AnyObject) {
// guard let feed = representedObject as? Feed else {
// return
// }
//
// print("Not modified response for \(feed.url).")
}
}
// MARK: - Utility

View File

@ -8,6 +8,7 @@
import UIKit
import RSCore
import RSWeb
import Account
import UserNotifications
@ -16,6 +17,9 @@ var appDelegate: AppDelegate!
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate, UISplitViewControllerDelegate, UnreadCountProvider {
private static var urlSessionId = "com.ranchero.NetNewsWire-Evergreen"
private var backgroundUpdateTask = UIBackgroundTaskIdentifier.invalid
var window: UIWindow?
var faviconDownloader: FaviconDownloader!
@ -24,7 +28,6 @@ class AppDelegate: UIResponder, UIApplicationDelegate, UISplitViewControllerDele
var feedIconDownloader: FeedIconDownloader!
private let log = Log()
private var didDownloadArticles = false
var unreadCount = 0 {
didSet {
@ -41,6 +44,11 @@ class AppDelegate: UIResponder, UIApplicationDelegate, UISplitViewControllerDele
appDelegate = self
NotificationCenter.default.addObserver(self, selector: #selector(unreadCountDidChange(_:)), name: .UnreadCountDidChange, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(userDefaultsDidChange(_:)), name: UserDefaults.didChangeNotification, object: nil)
DownloadSession.sessionConfig = URLSessionConfiguration.background(withIdentifier: AppDelegate.urlSessionId)
DownloadSession.sessionConfig?.sessionSendsLaunchEvents = true
DownloadSession.sessionConfig?.shouldUseExtendedBackgroundIdleMode = true
// Initialize the AccountManager as soon as possible or it will cause problems
// if the application is restoring preserved state.
@ -89,10 +97,7 @@ class AppDelegate: UIResponder, UIApplicationDelegate, UISplitViewControllerDele
}
}
// UIApplication.shared.setMinimumBackgroundFetchInterval(AppDefaults.refreshInterval.inSeconds())
NotificationCenter.default.addObserver(self, selector: #selector(accountDidDownloadArticles(_:)), name: .AccountDidDownloadArticles, object: nil)
// NotificationCenter.default.addObserver(self, selector: #selector(userDefaultsDidChange(_:)), name: UserDefaults.didChangeNotification, object: nil)
UIApplication.shared.setMinimumBackgroundFetchInterval(AppDefaults.refreshInterval.inSeconds())
return true
@ -139,20 +144,26 @@ class AppDelegate: UIResponder, UIApplicationDelegate, UISplitViewControllerDele
// Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}
// func application(_ application: UIApplication, performFetchWithCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
//
// AccountManager.shared.refreshAll()
// while (!AccountManager.shared.combinedRefreshProgress.isComplete) {
// sleep(1)
// }
//
// if didDownloadArticles {
// completionHandler(.newData)
// } else {
// completionHandler(.noData)
// }
//
// }
func application(_ application: UIApplication, handleEventsForBackgroundURLSession identifier: String, completionHandler: @escaping () -> Void) {
DownloadSession.completionHandler = completionHandler
backgroundUpdateTask = UIApplication.shared.beginBackgroundTask {
DispatchQueue.global(qos: .background).async {
while (!AccountManager.shared.combinedRefreshProgress.isComplete) {
sleep(1)
}
UIApplication.shared.endBackgroundTask(self.backgroundUpdateTask)
self.backgroundUpdateTask = UIBackgroundTaskIdentifier.invalid
}
}
}
func application(_ application: UIApplication, performFetchWithCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
AccountManager.shared.refreshAll()
completionHandler(.newData)
}
// MARK: - Split view
@ -174,12 +185,8 @@ class AppDelegate: UIResponder, UIApplicationDelegate, UISplitViewControllerDele
}
}
// @objc func userDefaultsDidChange(_ note: Notification) {
// UIApplication.shared.setMinimumBackgroundFetchInterval(AppDefaults.refreshInterval.inSeconds())
// }
@objc func accountDidDownloadArticles(_ note: Notification) {
didDownloadArticles = true
@objc func userDefaultsDidChange(_ note: Notification) {
UIApplication.shared.setMinimumBackgroundFetchInterval(AppDefaults.refreshInterval.inSeconds())
}
// MARK: - API

@ -1 +1 @@
Subproject commit 4cc13cd6dd0ae4c9d2c95dbfda4f1d341e57b16d
Subproject commit 9af3d830aa8a6e03d4a992b43f0d1376144d1bd5