开发者

reachability and singleton

开发者 https://www.devze.com 2023-02-16 02:47 出处:网络
How can I make the reachability work as a singleton? Here is what I want to do ... I have an application.

How can I make the reachability work as a singleton? Here is what I want to do ...

I have an application. When it launches I want to have a singleton and work with the reachability in order to hold the network status. Then I want from other controllers to access the singleton and check the network status. For example I want every 开发者_如何学JAVAtime user clicks on one tab and the screen is shown to check the internet status and enable or disable a button.

I know the question is very generic but I am so confused. I am currently using reachability 2.2

Any tips or ideas or even guides will be appreciated.

And a final question.... how 'moral' is the singleton usage? And is it the best practice to be more close to MVC model?

Thanks again


Singletons are fine. Reachability can be a very sensible singleton. You should be aware of the doc "Creating a Singleton Instance" in Apple's Cocoa Fundamentals Guide, but read the text carefully. The code they present is seldom what you need and can cause more problems then it solves. It is only appropriate in cases where it is wrong for there to be multiple copies of the object (wrong because the object manages a unique resource for instance). In those cases, I tend to prefer asserting in -init rather than overloading allocWithZone: because I'd rather force the developer to make the correct call (+sharedInstance) rather than quietly fix it for them, and have them think they've created two instances when there is actually one (at least in cases where the singleton is mutable or holds state).

There is an excellent discussion of good singleton patterns in this question. I favor @Robbie Hanson's answer in my own code because it is cheaply thread-safe.

0

精彩评论

暂无评论...
验证码 换一张
取 消