diff --git a/Frameworks/Account/LocalAccount/LocalAccountDelegate.swift b/Frameworks/Account/LocalAccount/LocalAccountDelegate.swift index 6f71ebbaf..7e0470d0c 100644 --- a/Frameworks/Account/LocalAccount/LocalAccountDelegate.swift +++ b/Frameworks/Account/LocalAccount/LocalAccountDelegate.swift @@ -202,7 +202,7 @@ final class LocalAccountDelegate: AccountDelegate { // MARK: Suspend and Resume (for iOS) func suspendNetwork() { - refresher.cancelAll() + refresher.suspend() } func suspendDatabase() { @@ -210,6 +210,6 @@ final class LocalAccountDelegate: AccountDelegate { } func resume() { - // Nothing to do + refresher.resume() } } diff --git a/Frameworks/Account/LocalAccount/LocalAccountRefresher.swift b/Frameworks/Account/LocalAccount/LocalAccountRefresher.swift index da9d21b69..c1b50b519 100644 --- a/Frameworks/Account/LocalAccount/LocalAccountRefresher.swift +++ b/Frameworks/Account/LocalAccount/LocalAccountRefresher.swift @@ -15,6 +15,7 @@ import Articles final class LocalAccountRefresher { private var completion: (() -> Void)? + private var isSuspended = false private lazy var downloadSession: DownloadSession = { return DownloadSession(delegate: self) @@ -29,8 +30,13 @@ final class LocalAccountRefresher { downloadSession.downloadObjects(feeds as NSSet) } - public func cancelAll() { + public func suspend() { downloadSession.cancelAll() + isSuspended = true + } + + public func resume() { + isSuspended = false } } @@ -56,7 +62,7 @@ extension LocalAccountRefresher: DownloadSessionDelegate { } func downloadSession(_ downloadSession: DownloadSession, downloadDidCompleteForRepresentedObject representedObject: AnyObject, response: URLResponse?, data: Data, error: NSError?) { - guard let feed = representedObject as? WebFeed, !data.isEmpty else { + guard let feed = representedObject as? WebFeed, !data.isEmpty, !isSuspended else { return } @@ -86,13 +92,14 @@ extension LocalAccountRefresher: DownloadSessionDelegate { } func downloadSession(_ downloadSession: DownloadSession, shouldContinueAfterReceivingData data: Data, representedObject: AnyObject) -> Bool { - guard let feed = representedObject as? WebFeed else { + guard !isSuspended, let feed = representedObject as? WebFeed else { return false } if data.isEmpty { return true } + if data.isDefinitelyNotFeed() { return false }