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

@gwakie I use NetNewsWire for RSS (syncs with Reader, but with a much better interface). Can't help you with Wave.

about 1 day ago

Working with JDBC. Surprisingly, I don't want to murder it this time.

about 2 days ago

@peachmactweet Got any of the new trackpads yet? =P

about 2 days ago

Web Inspector > Firebug. There, I said it.

about 3 days ago

@notasausage Athens is amazing. There's a bunch of great houses for sale here.

about 6 days ago