1
votes

I have a singleton class which only serves as web service calls for my application. Suppose that class is named as MySingleTonClass. Now to get various data i call different methods of the class.

eg. [[MySingleTonClass sharedInstance] getDataFromServer];

Now my question is that, there are certain times when i got to make nested web service calls (calling API on completion of another API call). And i am using this only class to do all the API calls, and as this is a singleton, will it work? OR even i am calling this class's method on subsequent lines.

line 1 - [[MySingleTonClass sharedInstance] getDataFromServer];
line 2 - [[MySingleTonClass sharedInstance] getOtherDataFromServer];

Will this work too? From my understanding, an instance of a singleton will remain in memory only once. That is once instance of it at a time. Therefore if invoking sharedInstance while already a work is being done by the sharedInstance, (already in memory) work as planned? or should i change the WebServiceCall class to a normal class and create object everytime, as that would make the class have multiple objects at a time therefore making multiple API calls at the same time.

Please suggest.thanks in advance.

EDIT

The singleton class has AFNetworking methods implemented inside of them

2
You shouldn't make this as singleton class. Always you must create a new instance for your web service class.Bharath Vankireddy
@BharathVankireddy that is not always true. If you are using AFNetworking then a new instance is not always required. It hits all the api in the form of operations and adds it to its queue. These are than concurrently run.Ankit Srivastava

2 Answers

0
votes

If that shared resource is changed by APIs then you will need to handle change using synchronized so you can make sure that changes made to singleton is handled in synchronized manner

If that singleton object doesn't changed by APIs then there is no problem

0
votes

This may or may not work depending how the internal implementation is of these methods. For instance if the thread on which is this called is solely responsible for calling the API and getting the data then it will work, but this will be bad design.

The better solution will be to design your api's around blocks. In the completion block of one web service you can call the second service and so on. You should look at some code which does this.