Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UIActivityIndicatorView not showing until after loading done

I have a button on the currently navigated to viewcontroller, connected to an IBAction.

In the IBAction I create a UIActivityIndicatorView as usual, with [self.view addSubView], then load some pictures.

I've tried setNeedsDisplay on the indicator view, the view controller, and the window, but it still loads the pictures before showing the indicator, which of course is quite useless to me.

So I'm looking for a way to either force an instant redraw (which when I think a little more about it is unlikely to make work), or a way to load the pictures after the indicator has appeared, or a way to launch a separate thread or similar to start animating / show the indicator, or put the indicator in a separate viewcontroller and somehow force it to add/show itself before going on to the picture-loading.

Recommendations?

like image 282
Henrik Erlandsson Avatar asked Nov 04 '10 14:11

Henrik Erlandsson


1 Answers

What I do in this situation is spawn a new thread, which frees up the main thread to handle UI interaction while stuff is loading in the background.

First show the UIActivityIndicatorView, then spawn a new thread that loads the images, then on the last line of the method that is executed in the new thread, hide the UIActivityIndicatorView.

Here's an example:

//do stuff...
[activityIndicatorView startAnimating];
[NSThread detachNewThreadSelector:@selector(loadImages) toTarget:self withObject:nil];

In your loadImages method:

- (void) loadImages {
   NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
   //load images...
   [activityIndicatorView performSelectorOnMainThread:@selector(stopAnimating)];
   [pool drain];
}
like image 128
Jacob Relkin Avatar answered Oct 09 '22 15:10

Jacob Relkin