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.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…