Holding off pushing a new view until the activity indicator appears?

Discussion in 'iOS Programming' started by neiltc13, Feb 24, 2009.

  1. neiltc13 macrumors 68040


    May 27, 2006
    I have a pretty simple application design - a table view with a list of screenshot names. When you tap one it loads a new view where the image loads.

    I want the images to download from the web and I have managed it. However, when the user selects a table cell, the new view does not appear until all of the image data is downloaded.

    So, I wanted to put an activity indicator on screen. I've managed to get it to work, but the problem is that it only appears just as the view is animating to the next view. So, really, it seems like it too is waiting for the data in the new view to download before displaying.

    - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    	[activityIndicator startAnimating];
    	BMWAppDelegate *appDelegate = (BMWAppDelegate *)[[UIApplication sharedApplication]delegate];
    	Shots *shots = (Shots *)[appDelegate.shots objectAtIndex:indexPath.row];
    	//------Below if statement may be required for memory management but removed to get a new view to load
    	//------each time and thus load the image that the user selects from the table
    	//if (self.shotView == nil){		
    	ShotViewController *viewController = [[ShotViewController alloc] initWithNibName:@"ShotViewController" bundle:[NSBundle mainBundle]];
    	self.shotView = viewController;
    	self.shotView.hidesBottomBarWhenPushed = YES; //Hide the tab bar when viewing an image
    	self.shotView.imgPath = [shots imgloc]; //Pass the URL of the image from the array to the view controller
    	[viewController release];
    	[self.navigationController pushViewController:self.shotView animated:YES];
    	//self.navigationController.navigationBar.barStyle = UIBarStyleBlackTranslucent;
    	self.shotView.title = [shots name];
    	[self.shotView.shotDescription setText:[shots description]];
    All I really need to know is how to display the activity indicator before proceeding with the rest and loading the new view. It's driving me up the wall, I didn't expect this problem when I started out and the things I thought would be difficult are turning out to be easy :p
  2. jnic macrumors 6502a

    Oct 24, 2008
    This is because (presumably) you're doing the data fetch in the main thread, which also happens to be the one used for UI updates. This means that while the fetch is running, the UI thread is blocked from doing updates, so your spinner doesn't appear until it's done.

    The solution is to perform your data fetch in another thread, and call back the main thread when you're done. Take a look at performSelectorInBackground and performSelectorOnMainThread respectively (both in NSObject). You'll need to use the latter to make any UI updates from other threads or they'll be ignored.
  3. neiltc13 thread starter macrumors 68040


    May 27, 2006
  4. flaminfish macrumors newbie

    Feb 11, 2010
    neiltc13, I'm working with very similar code, and was curious as to how you got your activity indicator implemented. (I'm assuming you got it to work as is evident of your smiley)

Share This Page