Consider a class with these methods:
- (id) initWithFrame: (CGRect) frame
if (!(self = [super init]))
webView = [[UIWebView alloc] initWithFrame:frame];
lock = [[NSConditionLock alloc] initWithCondition:LOCK_WAIT];
- (void) setHTML: (NSString *) html
[webView loadHTMLString:html baseURL:nil];
- (void)webViewDidFinishLoad:(UIWebView *)aWebView
// Locking to be able to unlock and change the condition.
webView = [[SynchronousUIWebView alloc] initWithFrame:frame];
You cannot use a
UIWebView from another than the main thread. So you could leave out all your locks and just let the web view and the run loop handle your delegate calling. The delegate methods should then be called from the main thread, too.
main-thread-only problem exists on a couple of Cocoa classes, including all of UIKit. It means, that all these classes' implementations rely on being called from the main thread only (for example, they use the main thread's run loop implicitly). It's simply not possible to use them on other threads, even when implementing your own locking.