-
-
Hi! I'm Michael Tyson, and I run A Tasty Pixel from our home in the hills of Melbourne, Australia. I write on a variety of technology and software development topics. I've also recently returned from a 3.5-year journey around Europe in a motorhome.
I make Loopy, the live-looper for iOS, Audiobus, the app-to-app audio platform, and The Amazing Audio Engine, the iOS audio framework.
Avoid the clobber: Nest your network activity indicator updates
On the iPhone, when you are doing anything that uses the network, you’re supposed to let the user know something’s going on, via
-[UIApplication setNetworkActivityIndicatorVisible:]
. This takes a boolean.That’s all well and good, but if you have more than one object in your app that may do things with the network simultaneously, you’re going to clobber yourself.
A nice and easy solution: Maintain an activity counter and create a category on UIApplication to maintain it, and show or hide the indicator as appropriate. Then, whenever you start doing something with the network:
…And when you’re done:
Here’s a category that’ll do it: