iPhone - implementing reachability for multiple hosts?
-
20-09-2019 - |
Question
I'm doing an app that will have to connect to several hosts. Originally I didn't include Apple's Reachability class, and just did it on my own synchronously, which I've since learned is a bad idea, and in practice I've seen the problems that Apple warns of. So now I'm going to do it asynchronously.
The problem lies in having several hosts - I see that in version 2.0 release notes, the following:
-Rewrote Reachability object to be fully asychronous and simplify monitoring of multiple SCNetworkReachabilityRefs.
So my question is - what did they do and what's the best way for me to take advantage? From googling around, I think it's that the Reachability class is no longer a singleton, and that I should create a new instance of Reachability for each host that I want to track. Is this correct?
Solution
Yes, that is correct - the AppDelegate in the sample code:
http://developer.apple.com/iPhone/library/samplecode/Reachability/listing4.html
Is using a number of instances, the key is that the notification you subscribe to for Reachability changes includes an object that is the instance of Reachability for which the status has changed - so if it cannot reach one particular host, that Reachability object would fire a notification. It does mean you have to keep track of your reachability instances somewhere to compare against this returned object (and to release them later as needed).