Reachability change notification should be called only once

I use Reachability in my fast-paced project. I had the code below in AppDelegate

NSNotificationCenter.defaultCenter().addObserver(self, selector: "reachabilityChanged:", name: ReachabilityChangedNotification, object: reachability) reachability.startNotifier() 

He calls

 func reachabilityChanged(note: NSNotification) { } 

But my problem is that it is called for the whole request. That is, I download images from the server, so when the network availability gets a change, this is the get call method for the whole request. I want this method to be called only once.

I also tried adding this notification and method to the ViewController, but this did not work.

Any help would be appreciated. thanks in advance

+4
source share
1 answer

You can add flags to prevent code execution if the status has not changed as follows:

 var connectionState = "Connected" let connectedState = "Connected" let notConnectedState = "notConnected" func checkForReachability(notification:NSNotification) { let networkReachability = notification.object as! Reachability; var remoteHostStatus = networkReachability.currentReachabilityStatus() if remoteHostStatus.value == NotReachable.value && connectedState == connectedState { connectionState = notConnectedState println("State Changed Not Connected") } else if remoteHostStatus.value == ReachableViaWiFi.value && connectedState == notConnectedState { connectionState = connectedState println("State Changed Connected") } } 
+2
source

All Articles