Start spinner before starting process on main thread?
I have this method:
-(IBAction)clickedDone; {
UIActivityIndicatorView *spinner = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
spinner.center = CGPointMake(self.view.frame.size.width / 2, self.view.frame.size.height / 2);
[spinner startAnimating];
//intensive stuff here
}
However, the spinner doesn't have time (I presume) to start before the intensive stuff comes afterward which blocks the main thread I guess. I want to know if there's a way to get this spinner animating before the thread is blocked. It'd be too much work to refactor my code to run the "intensive stuff" in a detached thread. The only alternative I can think of would be to delay the "intens开发者_如何学运维ive stuff" for a fraction of a second in order for the spinner to start.
What do you all think?
I think you need to put the expensive stuff on a second thread. Otherwise you block the interface as you say. It may be more work, but thats better than a bad user experience.
The spinner can only spin if the main thread is not being blocked. Therefore, you have to run "intensive stuff" on a different thread. Also, in order to delay "intensive stuff", you would have to add it to the operation queue for the thread, allowing the run loop to start over. If you are already planning that, then you might as well put it on a different thread.
First of all, you need to add it to the view:
[self.view addSubview:spinner];
Maybe that fixes things already.
精彩评论