Reachability.swift icon indicating copy to clipboard operation
Reachability.swift copied to clipboard

Strong reference needed not documented

Open cpellet opened this issue 4 years ago • 3 comments

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

cpellet avatar Jun 09 '20 16:06 cpellet

@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()

RubenVot avatar Jun 23 '20 15:06 RubenVot

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.

cpellet avatar Jun 24 '20 04:06 cpellet

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 为什么回调还是不走?

ChinaKeep avatar Jun 30 '21 07:06 ChinaKeep