How to write the method to execute after completion of two methods (ios)
我有2种方法可以在按钮单击事件中执行,例如
我必须在完成method1:和method2:
之后执行另一个方法
1 2 3 4 5 6 7 8 9 | -(void)doSomething { [method1:a]; [method2:b]; //after both finish have to execute [methodFinish] } |
除了典型的
,我该如何实现?
阅读有关积木的知识..我对积木很陌生。有人可以帮我写一个积木吗?任何解释都将非常有帮助。谢谢
这是调度组的用途。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | 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] }); |
调度组受ARC管理。它们会一直保留到系统中,直到所有块都运行为止,因此在ARC下可以轻松进行内存管理。
如果要在组完成之前阻止执行,另请参见
我从Google的iOS框架中获得的几乎没有什么方法,他们非常依赖:
1 2 3 4 5 6 7 8 | - (void)runSigninThenInvokeSelector:(SEL)signInDoneSel { if (signInDoneSel) { [self performSelector:signInDoneSel]; } } |