ashleymills / Reachability.swift

Replacement for Apple's Reachability re-written in Swift with closures
MIT License
7.94k stars 948 forks source link

Strong reference needed not documented #363

Open cpellet opened 4 years ago

cpellet commented 4 years ago

Please document the file level strong reference of the reachability object, notifier callbacks do not work if defined in viewDidLoad()

RubenVot commented 4 years ago

@cpellet this is already written in the docs

//declare this property where it won't go out of scope relative to your listener
let reachability = try! Reachability()
cpellet commented 4 years ago

In this case allow me to suggest to make this clearer; going out of scope is not the issue here, interacting with the object in a single clause will not solve the issue as the comment suggests.

ChinaKeep commented 3 years ago

let reachability = try Reachability()

class PXX_NetWorkTypeTool : NSObject{

// static let shared = PXX_NetWorkTypeTool() // private override init() { // super.init() // }

 func getNetType() -> Int {
    var ClientNetType:Int = 0
    reachability?.whenUnreachable = { _ in
        DispatchQueue.main.async {
            print("Not reachable")
            ClientNetType = 1
        }
    }
    do {
        try reachability?.startNotifier()
    } catch {
        print("Unable to start notifier")
    }
    reachability?.whenReachable = { reachability in

写在工具类中,全局属性reachablity 为什么回调还是不走?