如何正确multithreading? UIAlertView不显示,只显示灰屏
我已经在我的项目中实现了textToSpeech,并希望在说出文本时显示警报视图。 我在这里调用textToSpeech的方法:
//-----before TTS starts i try to display alertView with Cancelbutton //[self performSelectorOnMainThread:@selector(alertWhileTTS) withObject:nil waitUntilDone:YES]; //gray view and no alertview //[self performSelector:@selector(alertWhileTTS)]; //gray view and no alertview //[self alertWhileTTS]; //gray view and no alertview //this part here is blocking, no gray screen, //after TTS is ready, the alertView is displayed dispatch_async(dispatch_get_main_queue(), ^{ //Update UI if you have to [self alertWhileTTS]; }); [[self view] setNeedsDisplay]; [self synthesizeInBackground]; [queue waitUntilAllOperationsAreFinished]; [self setIsSpeaking: false]; [[self view] setNeedsDisplay];
这里的synthesizeInBackground方法( 在方法 synthesize
启动TTS ):
- (void) synthesizeInBackground { queue = [[NSOperationQueue alloc] init]; operation = [[NSInvocationOperation alloc] initWithTarget:self selector:@selector(synthesize) object:nil]; [queue addOperation: operation]; }
虽然TTS我想显示带cancel
按钮的alertView。 但在我的情况下,我只得到一个没有alertView的灰色屏幕。
如何正确调用alertWhileTTS ,以便显示alertView?
这是alertWhileTTS的内容:
- (void) alertWhileTTS { UIAlertView *inboxRead = [[[UIAlertView alloc] initWithTitle:@"Inbox tts..." message:nil delegate:self cancelButtonTitle:@"Abbrechen" otherButtonTitles:nil] autorelease]; inboxRead.tag = 997; [inboxRead show]; }
更新看到我的解决方案,这是有效的:
[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]; });
更改alertWithTTsTo
UIAlertView *inboxRead = [[[UIAlertView alloc] initWithTitle:@"Inbox tts..." message:nil delegate:self cancelButtonTitle:@"Abbrechen" otherButtonTitles:nil] autoRelease]; inboxRead.tag = 997; [inboxRead show];
也不要忘记从主ui线程调用函数alertWhileTTS
做
dispatch_async(dispatch_get_main_queue(), ^{ //Update UI if you have to [self alertWhileTTS]; });
您应该使用自动引用计数(ARC)因为这将自动释放所有内容。 正如borrden所说,你(大概)可以快速发布UIAlertView。