I have a UITableViewController and when I push a particular view onto the stack it takes forever so I wanted to add a spinner to the cell before moving along. The problem I’m having is that the spinner gets added after the new view gets pushed onto the controller stack. But I thought that messages were synchronous?
So how can I make this spinner display before moving onto the next view? Thanks in advance!
- (void)
tableView: (UITableView *) tableView
didSelectRowAtIndexPath: (NSIndexPath *) indexPath {
UITableViewCell *cell = [ self.tableView cellForRowAtIndexPath: indexPath ];
if (cell.accessoryType == UITableViewCellAccessoryDisclosureIndicator) {
UIActivityIndicatorView *activityIndicator = [ [ UIActivityIndicatorView alloc ] initWithFrame: CGRectMake(260.0, 10.0, 25.0, 25.0) ];
activityIndicator.hidesWhenStopped = YES;
activityIndicator.activityIndicatorViewStyle = UIActivityIndicatorViewStyleGray;
[ cell addSubview: activityIndicator ];
[ activityIndicator startAnimating ];
TableDemoViewController *newViewController = [ [ TableDemoViewController alloc ] initWithPath: cell.textLabel.text ];
UINavigationController *navigationController = [ appDelegate navigationController ];
[ navigationController pushViewController: newViewController animated: YES ];
[ activityIndicator stopAnimating ];
}
}
If simply initing TableDemoViewController is what’s so intensive, you’ve got other problems. Remember, the main thread is synchronous, as you pointed out. You have a couple options:
performSelector:withObject:afterDelay:with a delay of 0.In general, if you have something that’s blocking processing on the main thread like this, that’s bad. You want to move that off the main thread ASAP. The main thread is where input events and animations happen — the ability of the main thread to run freely is what keeps your app responsive and snappy. Remember, even if the user can’t advance the state of your application, there’s probably a lot she can interact with and a lot she can see.