Jak napisać metodę do wykonania po zakończeniu dwóch metod (ios)

Mam 2 metody do wykonania na przycisku click event say method1: i method2:.Oba mają połączenia sieciowe, więc nie można być pewnym, który z nich zakończy się pierwszy.

Muszę wykonać inną metodę methodFinish Po zakończeniu obu metod1: i method2:

-(void)doSomething
{

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

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

Jak mogę to osiągnąć poza typowym start method1:-> completed -> start method2: ->completed-> start methodFinish

Poczytaj o blokach..Jestem nowy w blokach.Czy ktoś może mi pomóc w napisaniu takiego?I każde Wyjaśnienie będzie bardzo pomocne.Dziękuję

Author: Lithu T.V, 2013-03-03

3 answers

Po to są grupy dyspozytorskie .

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]
});

Grupy dyspozytorskie są zarządzane ARC. Są one przechowywane przez system do czasu uruchomienia wszystkich bloków, więc zarządzanie pamięcią jest łatwe pod ARC.

Zobacz także dispatch_group_wait() jeśli chcesz zablokować wykonanie do zakończenia grupy.

 52
Author: Rob Napier,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2013-03-03 14:19:41

Zgrabna mała metoda, którą dostałem od googli iOS Framework, na którym dość mocno polegają:

- (void)runSigninThenInvokeSelector:(SEL)signInDoneSel {


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

}
 0
Author: ChuckKelly,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2016-05-08 06:54:03

Możesz po prostu użyć flagi (aka zmiennej BOOL), która pozwala Ci wiedzieć w obu metodach (A lub B), czy druga już się zakończyła. Coś na ten temat:

- (void) methodA
{
    // implementation

    if (self.didFinishFirstMethod) {
        [self finalMethod];
    } else {
        self.didFinishFirstMethod = YES;
    }
}


- (void) methodB
{
    // implementation

    if (self.didFinishFirstMethod) {
        [self finalMethod];
    } else {
        self.didFinishFirstMethod = YES;
    }
}


- (void) finalMethod
{
    // implementation
}
Zdrówko!
 -7
Author: Zoltán Vári,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2013-03-03 13:56:12