Mulithreading: making method calls only after completing another method

I am trying to process the method asynchronously, according to the requirements, as soon as the first method is completed, only then should the second method begin. The problem is that the first method itself has code that runs in the background thread.

I tried dispatch_semaphore_wait, but that didn't work either.

dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0); dispatch_group_t group = dispatch_group_create(); dispatch_group_async(group, queue, ^{ [self firstMethod]; NSLog(@"firstMethod Done"); }); dispatch_group_notify(group, queue, ^ { NSLog(@"1st method completed"); NSLog(@"2nd method starting"); [self secondMethod]; }); 

FirstMethod itself runs on a different workflow like this

 -(void)firstMethod { dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^{ //processing here..... }]; 

What is the best way to achieve it, I cannot change the definition of firstMethod, as provided by some third party, and also to change this means changing a large amount of existing code from which this method is called

+4
source share
2 answers

You can use the completion block. You just need to change firstMethod like this:

 - (void)firstMethodWithOnComplete:(void (^)(void))onComplete { dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^{ //processing here..... onComplete(); }); } 

And then use it like this:

 [self firstMethodWithOnComplete:^{ [self secondMethod]; }]; 
+12
source

Send a single queue and call your methods in order

 dispatch_group_async(group, queue, ^{ [self firstMethod]; NSLog(@"firstMethod Done"); [self secondmethod]; }); 

Or you can send a group of three simultaneous queues (this is a wild guess)

-1
source

Source: https://habr.com/ru/post/1480000/


All Articles