Provided with your code example I was able to use Reachability for a while now, but recently I've found that there is a leak which points to "try reachability.startNotifier()" using Debug Memory Graph.
class ConnectionManager: NSObject {
static let sharedInstance = ConnectionManager()
let reachability = Reachability()!
let network = NetworkLoader()
func observeReachability() {
NotificationCenter.default.addObserver(self, selector: #selector(self.reachabilityChanged), name: NSNotification.Name.reachabilityChanged, object: reachability)
do {
try reachability.startNotifier()
} catch(let error) {
print("Error occured while starting reachability notifications : \(error.localizedDescription)")
}
}
func stopReachability() {
reachability.stopNotifier()
NotificationCenter.default.removeObserver(self, name: .reachabilityChanged, object: reachability)
}
@objc func reachabilityChanged(note: Notification) {
guard let reachability = note.object as? Reachability else { return }
switch reachability.connection {
case .cellular:
self.network.hideNetworkAlert()
print("Network available via Cellular Data.")
case .wifi:
self.network.hideNetworkAlert()
print("Network available via WiFi.")
case .none:
self.network.showNetworkAlert(reachability: self.reachability)
print("Network is not available.")
}
}
}
class AppDelegate: UIResponder, UIApplicationDelegate {
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
ConnectionManager.sharedInstance.observeReachability()
return true
}
}
What version of Reachability.Swift are you using?
4.3.1
How is it installed? Manually, CocoaPods, Carthage?
CocoaPods
Does it occur on device or simulator? Which device?
Both, physical device - iPhone XS
What OS version are you running on?
12.1.3
Are there steps you can take to reproduce it?
Oddly I first observed the very same behavior.
I also put Reachability into a Singleton, just like you.
After a while I realized, that I just forgot NotificationCenter.default.removeObserver
on deinit inside a Super Class I use.
Oddly I first observed the very same behavior.
I also put Reachability into a Singleton, just like you.
After a while I realized, that I just forgotNotificationCenter.default.removeObserver
on deinit inside a Super Class I use.
Well, exactly the same happens when using closures, so I suppose this doesn't relate to clearing observers.
Oddly I first observed the very same behavior.
I also put Reachability into a Singleton, just like you.
After a while I realized, that I just forgotNotificationCenter.default.removeObserver
on deinit inside a Super Class I use.Well, exactly the same happens when using closures, so I suppose this doesn't relate to clearing observers.
excuse me锛學hen will this problem be solved?
@cxxcapf This is an open source project, so please feel free to contribute a fix if you have one.
Most helpful comment
@cxxcapf This is an open source project, so please feel free to contribute a fix if you have one.