I want to load a PDF file in a modal view controller.
Since the moment that I don’t know the size of the file, I want to load it on a separate thread, so:
- (void)viewDidLoad {
[super viewDidLoad];
[[UISharedApplication sharedApplication] setNetworkActivityIndicatorVisibile:YES];
[NSThread detachNewThreadSelector:@selector(loadPDF) toTarget:self withObject:nil];
}
- (void)loadPDF {
NSAutoreleasePool *threadPool = [[NSAutoreleasePool alloc] init];
[self.webView loadRequest:[NSURLRequest requestWithURL:url]];
[threadPool release];
[self performSelectorOnMainThread:@selector(showPDF) withObject:nil waitUntilDone:NO];
}
- (void)showPDF {
[self.webView setHidden:NO];
[self.spinner stopAnimating];
[[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO];
}
The method works pretty good in other files (at least I guess) where I parse some XML data, but here doesn’t. Commenting:
[self performSelectorOnMainThread:@selector(showPDF) withObject:nil waitUntilDone:NO];
lets me to see the spinner and the activity indicator on the top. So I think that:
[self.webView loadRequest:[NSURLRequest requestWithURL:url]];
doesn’t wait until finish and gives the control back to the caller. How can I fix it?
You can’t/shouldn’t (have got it to work once) update the UI on a separate thread, you should to it all on the main. As I believe webView is a UIWebView this is probably the reason.
Although you are changing the webview from hidden to visible on the main thread, all the updating and creating of the actual UIWebView is not.
Best thing to do is create the data from the URL then
performSelectorOnMainThreadand pass the data to a method on the main thread that updates the UIWebview with the data.