23

ボタンクリックイベントで実行する2つのメソッドがmethod1:あり、method2:どちらもネットワーク呼び出しがあるため、どちらが最初に終了するかわかりません。

methodFinishmethod1: と method2: の両方が完了したら、別のメソッドを実行する必要があります。

-(void)doSomething
{

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

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

典型的な以外にこれをどのように達成できますかstart method1:-> completed -> start method2: ->completed-> start methodFinish

ブロックについて読んでください..私はブロックに非常に慣れていません.誰かがこれを書くのを手伝ってくれますか?そして、どんな説明でも非常に役に立ちます.ありがとう

4

2 に答える 2

52

これがディスパッチグループの目的です。

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 でのメモリ管理は簡単です。

dispatch_group_wait()グループが終了するまで実行をブロックする場合も参照してください。

于 2013-03-03T14:11:53.807 に答える
0

彼らがかなり大きく依存しているGoogleのiOSフレームワークから得たきちんとした小さなメソッド:

- (void)runSigninThenInvokeSelector:(SEL)signInDoneSel {


    if (signInDoneSel) {
        [self performSelector:signInDoneSel];
    }

}
于 2016-05-08T06:54:03.037 に答える