Manni
Manni

Reputation: 11148

How to force main thread to process everything? Problem with UIWebView

I want to load some HTML in a UIWebView:

- (IBAction)buttonClicked:(id)sender {
    [webView loadHTMLString:@"Hello World" baseURL:nil];
    // a lot of other time-consuming code that has to perform in main thread
}

The problem is, that the UIWebView starts loading AFTER the "other time-consuming" code is finish. I want, that the UIWebView starts loading directly. Does the UIWebView wait for the main thread and starts loading after all events in the main thread are processed?

Can I force the main thread to process everything?

- (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: The following workaround works, but I think it isn't the best solution ;-)

- (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    
}

Upvotes: 0

Views: 1657

Answers (2)

b123400
b123400

Reputation: 6118

Use the same thread to process everything (especially time consuming stuffs) is not a good idea.

When you are doing something time consuming on the thread responded for UI, the whole UI will be frozen, and it will not be able to give any response to the user, e.g. buttons will not change color when you touch it. So you should separate your time consuming stuffs and UI stuffs on different threads.

Next, the UIKit is not thread-safe (except drawing to a graphics context), which means you should not do anything related to the UI outside of the main thread.

So the conclusion will be: Process your time consuming stuffs on another thread, not the main thread. This parts is very easy with iOS4's GCD, the following code should do:

#include <dispatch/dispatch.h>

[webView loadHTMLString:@"Hello World" baseURL:nil];
dispatch_queue_t myQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
dispatch_async(myQueue, ^{
    //Do something time consuming here
});

Note that the default queue in the above example will process tasks concurrently, if you want to manage the queue(s) yourself, you may create it like this:

dispatch_queue_t myQueue = dispatch_queue_create("com.mycompany.myqueue", 0);

More details can be found on Session 211 of WWDC 2010 Session Videos: Simplifying iPhone App Development with Grand Central Dispatch

Upvotes: 1

highlycaffeinated
highlycaffeinated

Reputation: 19867

I doubt you can force the main thread to handle the work of the UIWebView, but you can wait until the UIWebView is done before performing your "other time-consuming code". Set the delegate for the UIWebView and implement webViewDidFinishLoad:, having it call a method to perform the time-consuming task(s).

Upvotes: 3

Related Questions