RRZ Europe
RRZ Europe

Reputation: 954

iOS4 networkActivityIndicatorVisible blocked through UI

I am doing a request to download some images then I want to replace the subview with them. This means that the UI is blocked and then the new view displays sometime later.

I want the user to understand that the blocking occurs due to the download.

First I tried to use

[UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
[self downloadFunction];
[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;

That leads to maxium a blink (mostly nothing) of the Activity Indicator, since my request is blocking the UI thread.

I cannot put the downloadFunction to the background, since I depend on the downloaded data to be available before i push the controller (would lead to the Error "Pushing the same view controller instance more than once is not supported" since I could click the button more than once).

Then I tried to put a subview with a spinning wheel on top of the view:

[self performSelectorInBackground:@selector(showActivitySubView) withObject:nil];
or  
[NSThread detachNewThreadSelector: @selector(showActivitySubView) toTarget:self withObject:nil];

but still the UI is blocked and my indicator is just displayed after the download finished...

Any ideas?

Upvotes: 0

Views: 2004

Answers (1)

Count Chocula
Count Chocula

Reputation: 1041

You can simply split your method in two and use a timed call to allow the UI refresh thread to pick up before you block the main thread. For example:

- (void) doActualWork {
  [self downloadFunction];
  [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
}

- (void) doWork {
  [UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
  [self performSelector:@selector(doActualWork) withObject:Nil afterDelay:0.05];
}

There's probably a better way of doing this, and I'm not sure that the little indicator on the task bar would be enough activity indication to keep the app review folks happy, but this should work and won't require threading.

Upvotes: 1

Related Questions