Browse Source

[Issue #2677] Fixed reachability listener issue in airplane mode

Prior to this change, the reachability listener was notified at launch if the device was not in airplane mode. When in airplane mode, the reachability flags were 0, which is what we were defaulting the previous flags to. Because of this, the notifyListener function would early out since the flags had not changed.

The core issue here was that the default value of previousFlags was poorly chosen. It should instead default to a value that represents and unknown status. One way to do this would be to make previousFlags an optional. However, this change would not be backwards compatible, so it was abandoned.

The best backwards compatible way to make this change was to set previousFlags to a rawValue that is not currently used. That way, we can guarantee that the originally computed flags will be different than the default previous flags. This change results in the listener being called at launch even when in airplane mode.

Please refer to issue #2677 for more details.
Christian Noon 7 years ago
parent
commit
4711de7144
1 changed files with 5 additions and 3 deletions
  1. 5 3
      Source/NetworkReachabilityManager.swift

+ 5 - 3
Source/NetworkReachabilityManager.swift

@@ -128,7 +128,9 @@ open class NetworkReachabilityManager {
 
     private init(reachability: SCNetworkReachability) {
         self.reachability = reachability
-        self.previousFlags = SCNetworkReachabilityFlags()
+
+        // Set the previous flags to an unreserved value to represent unknown status
+        self.previousFlags = SCNetworkReachabilityFlags(rawValue: 1 << 30)
     }
 
     deinit {
@@ -157,8 +159,8 @@ open class NetworkReachabilityManager {
         let queueEnabled = SCNetworkReachabilitySetDispatchQueue(reachability, listenerQueue)
 
         listenerQueue.async {
-            self.previousFlags = SCNetworkReachabilityFlags()
-            self.notifyListener(self.flags ?? SCNetworkReachabilityFlags())
+            guard let flags = self.flags else { return }
+            self.notifyListener(flags)
         }
 
         return callbackEnabled && queueEnabled