indicator

The network activity indicator in iOS is the spinning wheel in the upper left of the screen in the status bar next to the carrier name and connection strength meters. The indicator spins when the device is using the network offering the user great feedback when they are using data. The indicator should be used for asynchronous network activity that will take more than a second or two. You can easily turn this on or off in your app when making network requests by setting a property on UIApplication. See the code below to simply turn the network activity indicator on or off.

This is a relatively simple interface API and it is very easy to turn the network activity indicator on and off while a network request is being performed. The drawback of this simple property is that if 2 asynchronous network requests are made and each is responsible for turning off the network activity indicator, then the first network request will turn off the indicator and the second request will still be in progress with no indicator. It can quickly become challenging to manage all running network activities without adding some more API to the activity indicator.

Allowing Multiple Threads/Requests to Use the Network Activity Indicator

The solution to the issue of the first request turning off the network activity indicator can be easily solved by tracking the number of running activities and only turning off the indicator after the last request has completed. This can be accomplished in iOS 7 by adding a category. Another alternative is to create a singleton to keep track of activities and turn the indicator on and off. In either case, the general implementation is the same, below is a demo of creating a singleton to manage the network activity monitor.

 

Public Interface

The class I created to manage the indicator is JSNetworkActivityIndicatorManager, here is the public interface.

 

Create the Singleton

Start by creating the sharedManager method to return the class instance.

 

Create the Start and End Methods

I use two properties in this class, the shared UIApplication and an integer called tasks to keep track of activities. The method to start an activity will turn on the indicator and increment the tasks property. The method to end an activity will decrement the tasks property and turn off the indicator if no tasks remain.

Note that I’m using the synchronize directive to make the class thread safe per Apple’s documentation. I also check to make sure the status bar is actually visible before messing with the network activity indicator.

Using the class is no more difficult than the UIApplication API.

Start a task.

In the network request completion handler end the task.

Clear all tasks from the manager and just turn off the indicator.

 

Complete Class Implementation

Here is the full implementation for the JSNetworkActivityIndicatorManager class.

Leave a Reply

Your email address will not be published. Required fields are marked *