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
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.
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.
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 .
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With