So I have this service class that utilize Restkit to consume REST web services. It is frequently used in my app. Instead of initialize it every time before using it, I create an object of this class and init it in appDelegate. But is this the best way to do so?
I thought about using singleton, but was a bit concerned about it in multi-threading environment. Any suggestions will be really appreciated. Thanks!
EDIT: I should mention that I work in ARC environment.
ARC doesn’t really matter. So long as you understand the negative aspects of singletons and are prepared to have that or deal with it, the pattern works fine.
What I suggest as an alternative is to design a static class and use the Provider pattern. This is a typical interface I use with RESTKit (this one accesses the facebook graph API).
There is no singleton, everything is static and if you design your interface to operate from your app data model and NOT your web service UI updates happen automatically.
Alternatively, if you feel you must use a singleton why not utilize your app delegate which is a true singleton. To expand, I am suggesting your web service or data provider becomes a member of an existing singleton and you will never have to worry about threading issues yourself because hopefully these are handled by Cocoa.