Skip to content

Commit

Permalink
Updates to using a reachability instance.
Browse files Browse the repository at this point in the history
  • Loading branch information
diegoreymendez committed Jun 3, 2019
1 parent 9886b92 commit 44d4a57
Showing 1 changed file with 11 additions and 22 deletions.
33 changes: 11 additions & 22 deletions WordPress/Classes/Uploads/UploadsManager.swift
Original file line number Diff line number Diff line change
@@ -1,27 +1,11 @@
import Foundation
import Reachability

/// Takes care of coordinating all uploaders used by the app.
///
class UploadsManager: NSObject {
private let uploaders: [Uploader]

/// The reason why this property is lazy is that it's basically a closure with a self reference.
/// There's no easy way to initialize these, other than making them lazy (2019-06-01)
///
private lazy var reachabilityObserver: NSObjectProtocol = {
return NotificationCenter.default.addObserver(forName: .reachabilityChanged, object: nil, queue: nil) { [weak self] notification in

guard let self = self else {
return
}

let internetIsReachable = notification.userInfo?[Foundation.Notification.reachabilityKey] as? Bool ?? false

if internetIsReachable {
self.resume()
}
}
}()
private let reachability: Reachability = Reachability.forInternetConnection()

// MARK: Initialization & Finalization

Expand All @@ -35,12 +19,17 @@ class UploadsManager: NSObject {

super.init()

/// This just makes sure the reachabilityObserver property is initialized here.
_ = reachabilityObserver
setupReachableBlock()
}

deinit {
NotificationCenter.default.removeObserver(reachabilityObserver)
// MARK: Reachability

private func setupReachableBlock() {
reachability.reachableBlock = { _ in
self.resume()
}

reachability.startNotifier()
}

// MARK: Interacting with Uploads
Expand Down

0 comments on commit 44d4a57

Please sign in to comment.