Singleton vs. third-party libraries

落爺英雄遲暮 提交于 2019-12-24 01:28:07

问题


I'm familiar with the concept of Singleton and this mechanism is pretty handy but..
What happens in case I want one, shared instance of some third-party class e.g. AFHTTPRequestOperation or maybe some Magical Record?
What should I do when I'm using one object from external class in many controllers? Or maybe it is a good practice to instantiate a new object in each controller?


回答1:


I'm not familiar with Magical Record but for AFNewtorking it definitely makes sense to make a singleton in many cases.

Though singleton of AFHTTPRequestOperation doesn't sound quite right. Much better candidate would be AFHTTPRequestOperationManager.

Quote from AFHTTPRequestOperationManager Class Reference

Developers targeting iOS 7 or Mac OS X 10.9 or later that deal extensively with a web service are encouraged to subclass AFHTTPSessionManager, providing a class method that returns a shared singleton object on which authentication and other configuration can be shared across the application.

For developers targeting iOS 6 or Mac OS X 10.8 or earlier, AFHTTPRequestOperationManager may be used to similar effect.

It was similar with AFHTTPCLient in <2.0 versions.

One good reference would also be this tutorial by Scott Sherwood on (as always) Ray Wenderlich blog.



来源:https://stackoverflow.com/questions/20773178/singleton-vs-third-party-libraries

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!