views:

153

answers:

1

I have a method that does a time consuming operation, say something like ten consecutive calls to [[NSString alloc] initWithContentsOfURL:u];

I want a UIActivityIndicatorView that was in a hidden state before the method call to show and animate, so I write:

activityIndicator.hidden = NO;
[activityIndicator startAnimating];

at the beginning of the method

but of course it won't work. The UIActivityIndicatorView will only animate once the method is over.

This is not acceptable. I must show the animation during the function call.

Anyone knows how to do it?

NSOperation maybe? (anyone has a sample thereof?)

+1  A: 

I assume you are doing some expensive work in this method and during that you wan't the activity indicator to spin? So you should't do that on the main thread anyway (It can happen that the iOS kills your app!). So put your expensive work on a separate thread. With e.g. - (void)performSelectorInBackground:(SEL)aSelector withObject:(id)arg and when the method (aSelector) is done call - (void)performSelectorOnMainThread:(SEL)aSelector withObject:(id)arg waitUntilDone:(BOOL)wait and there you stop the activity indicator. But never call any UI code from within a non main thread!

V1ru8