1

我想顯示一個alertview,而其他線程(在其他線程有文字到語音)。簡而言之,我想在同一時間調用兩種方法,並且也希望他們同時「工作」。相當於waitUntilAllOperationsAreFinished

但在我的情況下,我開始alertView,屏幕變暗,沒有alertview,其他線程開始。之後,其他線程準備好了,我得到的藍色alertview:

下面是代碼,我的問題是:什麼是waitUntilAllOperationsAreFinished相當於..?

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

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

也看到這個帖子:How to multithred correctly? UIAlertView is not displayed, only gray screen

回答

0

這解決了我的問題:

[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]; 

    });