Rectanglicle

The Personal Website of Ash M. White

iPhone OS: Perform Network Activity Immediately After Loading A View

by Ash White / 03.07.10

I just spent a good hour trying to diagnose a problem related to the view lifecycle in the iPhone OS, so I thought I'd share the solution here.

My problem was in trying to grab some data from the network immediately after loading a view from a UITabBar. The goal was to have it switch to the new view, issue the system network activity indicator, and update a UITableView when the data was done processing. Something like this:


- (void) viewDidLoad {
	[super viewDidLoad];
	
	[self updateData];
}

However, no matter whether I put the code in viewDidLoad or viewDidAppear, the entire interface would appear to freeze before switching to the new view and would not respond again until the data loaded. I didn't even notice the delay in the simulator using WiFi, but when I deployed to my iPhone and tested it over 3G the process took upwards of five seconds (which I find unacceptable from a UX standpoint).

I eventually figured out a solution, which was to use a tiny delay before starting the network activity. This is a simple task:


- (void) viewDidLoad {
	[super viewDidLoad];
	
	[self performSelector:@selector(updateData:) withObject:nil afterDelay:.1];
}

The afterDelay parameter defines how long the app will wait (in seconds) before issuing the method on the specified object. Note that you are forced to pass one parameter to the target method. If your target method doesn't need to take any arguments, simply pass nil as I did above. You will also need to update your method signature to take that parameter, even if you do nothing with it.

Categories

Tags

blog comments powered by Disqus
info

All work is original and copyright 2014 by Ash White, with the exception of a handful of free third-party libraries, including the Scriptaculous JavaScript framework, Lightbox image overlay, Google Font Directory, Google Code Prettify, and Eric Meyer's CSS Reset.