【发布时间】:2011-08-05 13:32:06
【问题描述】:
我想在 UIWebView 中加载一些 HTML:
- (IBAction)buttonClicked:(id)sender {
[webView loadHTMLString:@"Hello World" baseURL:nil];
// a lot of other time-consuming code that has to perform in main thread
}
问题是,UIWebView 在“其他耗时”的代码完成后开始加载。我想要 UIWebView 直接开始加载。 UIWebView 是否等待主线程并在处理完主线程中的所有事件后开始加载?
我可以强制主线程处理所有内容吗?
- (IBAction)buttonClicked:(id)sender {
[webView loadHTMLString:@"Hello World" baseURL:nil];
// !!! main thread, please PERFORM everything until here BEFORE going on !!!
// a lot of other time-consuming code that has to perform in main thread
}
PS:以下解决方法有效,但我认为这不是最佳解决方案;-)
- (IBAction)buttonClicked:(id)sender {
[webView loadHTMLString:@"Hello World" baseURL:nil];
[NSThread detachNewThreadSelector:@selector(startNewThread) toTarget:self withObject:nil];
}
- (void)startNewThread {
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
[NSThread sleepForTimeInterval:1];
[self performSelectorOnMainThread:@selector(goBackToMainthread) withObject:nil waitUntilDone:NO];
[pool release];
}
- (void)goBackToMainthread {
// a lot of other time-consuming code that has to perform in main thread
}
【问题讨论】:
标签: iphone objective-c multithreading uiwebview