access reachability with enough time to return true state

44 views Asked by At

i have a reachability class which is checked from within my VC code as below:

func setupReachability (hostName:String?, useClosures: Bool) {

    let reachability = hostName == nil ? Reachability() : Reachability(hostname: hostName!)
    self.reachability = reachability
    try! self.reachability?.startNotifier()

    if useClosures {
        reachability?.whenReachable = { reachability in
            DispatchQueue.main.async {
                self.connected = true
                print("Reachable....")
            }
        }
        reachability?.whenUnreachable = { reachability in
            DispatchQueue.main.async {
                self.connected = false
                print("Not Connected....")
            }
        }

    } else {
        NotificationCenter.default.addObserver(self, selector: Selector(("reachabilityChanged:")), name: ReachabilityChangedNotification, object: reachability)
    }
}

this is initialised from within viewDidLoad:

    setupReachability(hostName: nil, useClosures: true)
    print(self.connected)

the problem i am having is that it always returns false and i need this to load with enough time that it returns true (if it is really connected) at this point. I'm struggling to figure out where to place my setupReachability function and initialise it so that it can be accessed from anywhere in the app and return its connected state. Ive tried placing this in a separate class but can't seem to get it working or access the connected state. Any tips on how to achieve this?

0

There are 0 answers