考虑一个具有这些方法的类:
- (id) initWithFrame: (CGRect) frame
{
if (!(self = [super init]))
return nil;
webView = [[UIWebView alloc] initWithFrame:frame];
[webView setDelegate:self];
lock = [[NSConditionLock alloc] initWithCondition:LOCK_WAIT];
return self;
}
- (void) setHTML: (NSString *) html
{
[lock lockWhenCondition:LOCK_WAIT];
[webView loadHTMLString:html baseURL:nil];
[lock unlock];
}
- (void)webViewDidFinishLoad:(UIWebView *)aWebView
{
[lock lockWhenCondition:LOCK_WAIT];
// Locking to be able to unlock and change the condition.
[lock unlockWithCondition:LOCK_GO];
}
- (NSString *) stringByEvaluatingJavaScriptFromString: (NSString *) jsCommand
{
[lock lockWhenCondition:LOCK_GO];
NSString * res = [webView stringByEvaluatingJavaScriptFromString:jsCommand];
[lock unlock];
return res;
}
我们称这个类为SynchronousUIWebView。 从我执行的主线程:
webView = [[SynchronousUIWebView alloc] initWithFrame:frame];
[webView setHTML:html];
[webView stringByEvaluatingJavaScriptFromString:jsCommand];
问题似乎是在我离开当前调用堆栈之前不会调用委托(delegate),而我没有这样做,因为我正在等待委托(delegate)调用发生,也就是死锁。在我看来,委托(delegate)调用似乎被推送到当前调用完成时调用的队列。所以问题是我可以修改此行为吗?
注意:需要这样做的原因是在加载 HTML 之前我无法执行 JavaScript。
最佳答案
你不能从主线程以外的地方使用 UIWebView
。所以你可以省去所有的锁,让 WebView 和运行循环处理你的委托(delegate)调用。然后也应该从主线程调用委托(delegate)方法。
main-thread-only
问题存在于几个 Cocoa 类中,包括所有 UIKit。这意味着,所有这些类的实现都依赖于仅从主线程调用(例如,它们隐式地使用主线程的运行循环)。根本不可能在其他线程上使用它们,即使在实现您自己的锁定时也是如此。
关于ios - 在等待调用委托(delegate)时如何让主线程休眠?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2801884/