Search code examples
multithreadingasynchronousuialertviewnsoperationqueuedispatch

equivalent of waitUntilAllOperationsAreFinished


i want to show an alertview while other thread(in other thread there is text to speech). in short words, i want to call two methods at same time and also want them that both "work" at same time.

but in my case, i am starting alertView, the screen is dimmed and without alertview, the other thread gets started. after the other thread is ready, i am getting the blue alertview:

Here is the code and my question is: What is the equivalent to waitUntilAllOperationsAreFinished..?

dispatch_async(dispatch_get_main_queue(), ^{
        [self alertWhileTTS];
    });

    [[self view] setNeedsDisplay];
    [self synthesizeInBackground];
    [queue waitUntilAllOperationsAreFinished];
    [self setIsSpeaking: false];
    [[self view] setNeedsDisplay];

Also see this post: How to multithred correctly? UIAlertView is not displayed, only gray screen


Solution

  • this solves my problem:

    [self performSelectorOnMainThread:@selector(alertWhileTTS) withObject:nil waitUntilDone:YES];
    
        dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, (unsigned long)NULL), ^(void) {
            [[self view] setNeedsDisplay];
            [self synthesizeInBackground];
            [queue waitUntilAllOperationsAreFinished];
            [self setIsSpeaking: false];
            [[self view] setNeedsDisplay];
    
        });