开发者

iOS4 networkActivityIndicatorVisible blocked through UI

开发者 https://www.devze.com 2023-01-28 00:27 出处:网络
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 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 blo开发者_C百科cked and my indicator is just displayed after the download finished...

Any ideas?


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号