1

I want to call a web service to upload some image data to the server. I have to send the data 5 times to the server. This piece of code is written in a function which is called after 10 seconds duration by a timer. Now the problem is that the response of Web service might be late and second call to web service might initiate. I want to keep them in queue so that when one finishes other is called. I think I am not going in right way. I just want to maintain a queue in which I can call the web service multiple times and make different async calls to the server. Basically how I could call multiple async tasks.

Any help will be appreciated.

dispatch_queue_t myQueue;
myQueue = dispatch_queue_create("My Queue",NULL);
 dispatch_async(myQueue, ^{
                               [self uploadDataToServer];
                                dispatch_async(dispatch_get_main_queue(), ^{
                                    // Update the UI

                                });
                            });
Rajan Maheshwari
  • 14,465
  • 6
  • 64
  • 98
  • can you explain 'I just want to maintain a queue in which I can call the web service multiple times and make different async calls to the server' a bit. I understand that, you want to call sync calls in async thread (queue). Your method should do this (i am not sure but you may change async to sync). "Basically how I could call multiple async tasks." is that mean how can you create more queues? – kocakmstf Feb 26 '15 at 09:49
  • yes you can say that.. I ll explain the whole scenario.. I m collecting some gps data (tracking data) in interval of 10 seconds and then I have to send the data to the server after each collection (have to send data 5 times in 50 seconds). My UI shouldn't block also. Thats why using async. I just want to ask whether I am doing this correct. Can you give me some working example where there are 2 -3 aynch calls in iOS that hit some web service – Rajan Maheshwari Feb 26 '15 at 10:22
  • afaik there isn't any function to handle your condition like sending sync queue on async thread. But you can handle this with your own queue (real queue, not thread queue). You can create a class for this which has FIFO based array, then a async process which checks is there remaining task in that fifo array. I can preapera a sample you but i dont have my mac nearby. i can send it in 6-7 hours later. – kocakmstf Feb 26 '15 at 11:38
  • thanks a lot..if possible please send the sample whenever you are free... thanks a ton. – Rajan Maheshwari Feb 26 '15 at 11:46
  • @kocakmstf can you please send me any sample – Rajan Maheshwari Mar 03 '15 at 05:19
  • added as an answer. If i understand your wish exactly, this is it – kocakmstf Mar 03 '15 at 16:10

2 Answers2

0

A simple way is to keep a counter and recurse. It looks like your uploadToServer is a blocking call so e.g.

- (void)uploadDataToServerAndRepeat:(NSUInteger)repeatCount {
    if(repeatCount)
        dispatch_async(
            dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_NORMAL, 0),
            ^{
                [self uploadDataToServer];
                [self uploadDataToServerAndRepeat:repeatCount - 1];

                // dispatch async to main queue for UI update, too
            });
}

// ... and, to start things off ...
[self uploadDataToServerAndRepeat:5];
Tommy
  • 99,986
  • 12
  • 185
  • 204
  • The same principle applies. Upon completion, however reached (I think AFNetworking allows you to supply a completion block), repeat with a decremented repeat count. – Tommy Feb 27 '15 at 18:15
0

Added a sample project. Maybe there was any other way for it but you can simply use TaskQueue.m class in sample process. You can modify it if you wish. https://github.com/kocakmstf/AsyncTaskQueue

kocakmstf
  • 135
  • 12