I have an UIActivityIndicator that should loop until a PDF file is loading.
I can’t understand why, but the indicator disappears before the PDF is loaded.
I can’t understand if the PDF file is too large (1,4 MB), if my app freezes, or if I wrote something wrong in the code
-(void)startTheProcess {
act = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
//coordinate act
[act setCenter:CGPointMake(320 / 2, 440 / 2)];
self.act.hidden = FALSE;
[self.view addSubview:act];
[act startAnimating];
[UIApplication sharedApplication].networkActivityIndicatorVisible = TRUE;
[NSThread detachNewThreadSelector:@selector(creaVista) toTarget:self withObject:nil];
}
- (void) creaVista{
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
NSURL *url = [NSURL URLWithString:@"http://www.something.com/file.pdf"];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[pdfView loadRequest:request];
[pdfView setScalesPageToFit:YES];
[self performSelectorOnMainThread:@selector(processDone) withObject:nil waitUntilDone:NO];
[pool release];
}
-(void)processDone {
[act stopAnimating];
[UIApplication sharedApplication].networkActivityIndicatorVisible = FALSE;
}
If I debug it, I can easily see that the debugger goes quickly to [act stopAnimating] and then stops, but the file is not yet loaded: after a few seconds the PDF appears.
Have you got any hints? I’m a noob and I think I’m doing some foolish mistakes…
Thanks
It sounds like Joshua has the right idea in the comments. The load is asynchronous and will return before the load is actually done.
If your
pdfViewis a UIWebView you can use theUIWebViewDelegatemethodwebViewDidFinishLoad:to know when the PDF is done loading. If you place your[act stopAnimating]in there it should work.You would also want to place it in
webView:didFailLoadWithError:incase there are any errors.To set the delegate on the
pdfViewdo this:[pdfView setDelegate:self];and make sure that you have your controller adopt the
UIWebViewDelegatelike this:@interface ClassName : ItsSuperclass < UIWebViewDelegate >