rectanglicle

the personal website of ash m. white

07
Mar
2010

iPhone OS: Perform Network Activity Immediately After Loading A View

Filed under: iPhone, Objective-c

Tagged: views, network activity

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.

Comments

15
Jun
2010
benjo&dejan, at 10:35 am:

10x mate!

Leave a Comment

I am a software developer and designer in Athens, Georgia. I'm passionate about web development, graphic design, and web standards. I am currently enrolled in the University of Georgia Master of Internet Technology program. More...

@ashwhite

Dear @charlesproxy, You're amazing. Love, Ash

about 15 hours ago

Thanks to @johnallsopp for the very inspiring talk tonight about HTML5 and the future of web technology.

about 3 days ago

@Stormink Other bad changes aside, CDs are not analog. If the icon had started as a vinyl record, that would be a totally different story.

about 3 days ago

@koefod You're on a Mac, right? You should give this a try: http://agilewebsolutions.com/products/1Password

about 3 days ago

When method names include the word "kludge," it's a bad sign.

about 4 days ago