cojoj
cojoj

Reputation: 6475

Singleton vs. third-party libraries

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?

Upvotes: 2

Views: 358

Answers (1)

Rok Jarc
Rok Jarc

Reputation: 18865

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.

Upvotes: 1

Related Questions