Intro
Threading on any platform is important so that you can perform asynchronous operations.
The ability to perform 2 tasks at once allows you to run methods in the background without the entire application grinding to a halt waiting for the operation to complete.
This article will help explain how to achieve background processing on iPhone using Objective-C and explain how to use threading to implementing an activity indicator.
Elements of threading in Objective C
Here are the main elements you have to understand in order to perform threading in Objective-C.
The main method which you want the action to happen on, e.g. a button click method:
- (void)ButtonClickMethod: (id) sender
The method you want to run in the background:
- (void)BackgroundMethod: (id)sender
The method you want to run when the background method is finished:
Within the first main method (ButtonClickMethod in our example) that will be called upon an action taking place, you need to create an instance of the NSInvocationOperation class – this is the creates the request for a background method.
NSInvocationOperation *operation = [[NSInvocationOperation alloc] initWithTarget:self
selector:@selector(BackgroundMethod:) object:self];
Declaring this means that you have setup an action/task ready to take place, all you need to do now is add it into the queue of operations which then requests the operation get executed.
Declare the operation queue using the NSOperationQueue class and then to add the operation to the operation queue, you use the addOperation method:
NSOperationQueue opQueue = [[NSOperationQueue alloc] init];
[opQueue addOperation: operation];
When the BackgroundMethod code has been deemed finished, you need to include a call to the DoneMethod to allow you to clear up any unused objects, restore, and change the state of the view as you see fit:
[(ViewControllerName *)sender done];
This is the foundation requirements to be able to setup threading on iPhone in Objective C, but do ensure all of the objects manually allocated to memory are released at the correct times, as I have only simplified the code examples here.
Activity Indicator Example
The purpose of an activity indicator is to give a user an indication that something is happening behind the scenes and that they should wait for the operation to complete. Without using threading, the screen would freeze for the duration of the code being executed and then would suddenly appear again once it had completed.
This is standard activity indicator that is used on most iPhone apps:
In Objective C with iPhone development this is called a UIActivitiyIndicatorView and is best instantiated and added to the current form/view programmatically.
To use the Activity Indicator you must first instantiate it using;
UIActivityIndicatorView aivLoading = [[UIActivityIndicatorView alloc]
initWithActivityIndicatorViewStyleWhiteLarger];
You must then add it as a sub view of the current view;
[self.view addSubview: aivLoading];
From here you can start and stop the graphic animating by calling the startAnimating and stopAnimating methods.
With the previous example on the elements that make up the threading process in iPhone, you can see how this all fits together:
- (void)ButtonClickMethod: (id) sender
{
// Start our activity indicator spinning (assuming its already been declared and added as a
subview)
[aivLoading startAnimating];
// Create the call to the background method we want to run
NSInvocationOperation *operation = [[NSInvocationOperation alloc] initWithTarget:self
selector:@selector(BackgroundMethod:) object:self];
// Add the operation to the queue to start it running
NSOperationQueue opQueue = [[NSOperationQueue alloc] init];
[opQueue addOperation: operation];
}
- (void)BackgroundMethod: (id)sender
{
// *** Your code here ***
// Call our done method
[(ViewControllerName *)sender done];
}
- (void) DoneMethod
{
[aivLoading stopAnimating];
}
So you see here that the activity indicator gets started at the very beginning of the method call, then the background method runs in parallel with the rest of the operations going happening on-screen on the phone. When the background method has completed, the activity indicator is called upon to stop animating because the operation has completed.
This is how we achieve the graphic animation synchronously whilst another action is taking place.
Again this code example simplifies the code to make it clearer, you still must keep in mind the use of Objective-C objects memory management basics