Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to write the method to execute after completion of two methods (ios)

I have 2 methods to be executed on a button click event say method1: and method2: .Both have network calls and so cannot be sure which one will finish first.

I have to execute another method methodFinish after completion both method1: and method2:

-(void)doSomething
{

   [method1:a];
   [method2:b];

    //after both finish have to execute
   [methodFinish]
}

How can I achieve this other than the typical start method1:-> completed -> start method2: ->completed-> start methodFinish

Read about blocks..I am very new to blocks.Can anybody help me with writing one for this?And any explanation will be very helpful.Thank you

like image 997
Lithu T.V Avatar asked Mar 03 '13 13:03

Lithu T.V


People also ask

What is a method in Java?

A method is a collection of statements that perform some specific task and return the result to the caller. A method can also perform some specific task without returning anything. In this article, we will understand how to call a method that returns some other method in Java.

How to perform some task upon completion of some asynchronous task?

There are basically two ways to perform some task upon completion of some asynchronous task − But when the code includes dealing with many (more than one) asynchronous functions then the Promise.all function of the former has an edge over the latter.

Why do I need a methods section?

A methods section that enables other researchers to understand and replicate your results is a constant principle of rigorous, transparent, and Open Science. Aim to be thorough, even if a particular journal doesn’t require the same level of detail .

Why should I include methods in my journal article?

A methods section that enables other researchers to understand and replicate your results is a constant principle of rigorous, transparent, and Open Science. Aim to be thorough, even if a particular journal doesn’t require the same level of detail . Reproducibility is all of our responsibility.


1 Answers

This is what dispatch groups are for.

dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
dispatch_group_t group = dispatch_group_create();

// Add a task to the group
dispatch_group_async(group, queue, ^{
  [self method1:a];
});

// Add another task to the group
dispatch_group_async(group, queue, ^{
  [self method2:a];
});

// Add a handler function for when the entire group completes
// It's possible that this will happen immediately if the other methods have already finished
dispatch_group_notify(group, queue, ^{
   [methodFinish]
});

Dispatch groups are ARC managed. They are retained by the system until all of their blocks run, so their memory management is easy under ARC.

See also dispatch_group_wait() if you want to block execution until the group finishes.

like image 171
Rob Napier Avatar answered Nov 15 '22 13:11

Rob Napier