MoLowKey
MoLowKey

Reputation: 1144

how to make serialized async tasks

i want to run some operations task with completion continuation- sequentially

for example i want to execute getSomethingsWithResultWithCompletion method for 3 times as serialized tasks (like op1 depend op2 , ... depen op N ) :

[MFLayer getSomethingsWithResultWithCompletion:^(id  _Nullable response)Completion {

 // it will be run on another thread!** 
[MFRequestManager retrivesomeDataWithCompletion:^(id  _Nullable response1) {

 // it will be run on another thread!** 
  [MFRequestManager retriveAnothersomeDataWithInfo:response1 WithCompletion:^(id  _Nullable response2) {

      NSLog(@"Finished with Result : %@",response2);

 }];

 }];

}];

Problem

if retrive methods execute in another thread (like send a request with AFNetworking) i have a problem with serialize and another task will be start.

i have try With NSOperationQueue and Semaphore but still have a problem

i have implemented something like this with NSOperationQueue and NSOperation but implementation of them have run on the same thread so all of task start sequentially so it's works fine. operationQueueExample

Upvotes: 0

Views: 141

Answers (1)

Andrea
Andrea

Reputation: 26383

I strongly discourage that approach, but if you send the task on a background thread, you can use GCD semaphores.

dispatch_semaphore_t sema = dispatch_semaphore_create(0);

[MFRequestManager retrivesomeDataWithCompletion:^(id  _Nullable response) {
   if(Completion) 
    Completion(response)
    dispatch_semaphore_signal(sema);
 }];
dispatch_semaphore_wait(sema, DISPATCH_TIME_FOREVER);

Upvotes: 1

Related Questions