Question

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?

Was it helpful?

Solution

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];
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top