提交 4711de71 编写于 作者: C Christian Noon

[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.
上级 32573d05
......@@ -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
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册