Tuesday, March 23, 2010

iPhone Sequential Execution

Something that I've found a little odd in iPhone code is that things don't always execute sequentially. Actually, I'm sure they do, but because they all seem to load at once they don't seem like it. I found a great method of something executing right away. By encapsulating a method in the following function, things before it run, and then this executes and runs. This is especially useful to be so that I can have an activity indicator (spinning ball) run while this happens in the background.

// Start up our activity indicator while we collect the data

[self startActivityIndicator];

// Run the loadData method right now

[self performSelector:@selector(loadDataInBackground) withObject:nil afterDelay:0.0];

I can then end the Activity Indicator when I get to the end of the "loadDataInBackground" method. If we just call both of the methods, the ActivityIndicator doesn't display until just as the loadData method ends.