Activity Indicator Example with Thread



When you develop the Apps in iOS, there are some tasks which take longer time. You can not ignore the user while there is some processing happening in foreground or background. On Apple iPhones you can inform the user about such tasks with the help of Activity Indicator view. This object shows rotating lines to suggest the user to wait until the activity is completed.

I am trying to build very simple example with threading to show activity indicator usage. There will be three objects in your XIB or storyboard. UILabel to show incrementing number. Activity Indicator View & UIButton object to control the movement of numbers.

Note: I’ve used Xcode 4.5.x with iPhone Simulator for iOS6 in this example, however this example can work on older iOS versions without any change.

Activity Indicator with Thread Example

Step 1:  Create new Xcode Project as Single View Application

New Single View App Project in Xcode

Step 2: Go to Story board and design the App

Notice the Autoshrink property which is set to Minimum Font Size. UILabel object is stretched till corners with center alignment.

UILabel Object for Number output

You can drag and drop the Activity Indicator View from Objects Panel. Configure these attributes as mentioned below.

  • Hides when stopped
  • Hidden

Activity Indicator Object

Step 3: Add the IBOutlets and IBActions in ViewController.h

//
//  ViewController.h
//  ActivityThreadExample
//
//  Created by Deepak Keswani on 19/10/12.
//  Copyright (c) 2012 Deepak Keswani. All rights reserved.
//

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController
@property (weak, nonatomic) IBOutlet UIButton *button;

@property (weak, nonatomic) IBOutlet UILabel *noutput;
@property (weak, nonatomic) IBOutlet UIActivityIndicatorView *activity;
- (IBAction)clicked:(id)sender;

@end

Step 4: According to header declaration add the methods in ViewController.m file

//
//  ViewController.m
//  ActivityThreadExample
//
//  Created by Deepak Keswani on 19/10/12.
//  Copyright (c) 2012 Deepak Keswani. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

@synthesize button;
@synthesize noutput;
@synthesize activity;

BOOL running = NO;
int counter = 0;

- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view, typically from a nib.
}

- (void)viewDidUnload {
    [self setActivity:nil];
    [self setButton:nil];
    [self setNoutput:nil];
    [super viewDidUnload];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void) doCounting {
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0),^ {
        while (running) {
            sleep(1);
            counter++;
            dispatch_async(dispatch_get_main_queue(),^{
                [noutput setText:[NSString stringWithFormat: @"%d",counter]];
            });
        }
    });

}

- (IBAction)clicked:(id)sender {
    if (!running) {
        [activity startAnimating];
        running = YES;
        //[self performSelectorInBackground:@selector(doCounting) withObject:nil];
        [self performSelector:@selector(doCounting) withObject:nil afterDelay:1];
        [button setTitle:@"Stop" forState:UIControlStateNormal];
    }else {
        [activity stopAnimating];
        running = NO;
        [button setTitle:@"Start" forState:UIControlStateNormal];
    }

}

@end

 

Step 5: Create the connections between the display objects and methods

  • Connect the UILabel, UIButton & UIActivityIndicatorView object with noutput, button & activity respectively.
  • Connect the UIButton touchUpInsideEvent with clicked method.

Connection Inspector View

Step 6: Build and Run the project to see the Activity Indicator App running in Simulator or iPhone

Build & run the app for iPhone Simulator. If you have followed all the instructions well, you’ll see pressing the Start button in App shows the activity Indicator and increments the number every 1 second. Number is incremented and activity Indicator also rotates until the stop is pressed. When you press the start there is doCounting method invoked. doCounting method  has while loop with running boolean variable. To update the status of running variable async_dispatch is called to give opportunity to button to be pressed. updating the status of running variable with NO ends the while loop and incrementation of numbers stops. Activity Indicator has two methods startAnimating, stopAnimating and we have chosen to hide the activity Indicator when it is stopped.

Hope you find this simple example useful as per your needs.

 



About Deepak Keswani 82 Articles
Developing Applications for Computers since 1995 :)

5 Comments

  1. Sir, great post. I am currently using the same concept of Activity Indicator and GCD to load images in table view.

    I had one question though, how would i present activity indicator in every cell of the table view till the time image is loaded.

    Also, whenever I scroll the table view, instead of showing empty cells, the previous loaded image is displayed and is replaced with the new one as soon as the new image is loaded. How to fix this bug.

    It would be great if you could provide a tutorial on NSOperations Queue, although, NSOperations Queue is a SuperSet of GCD with some nice features but I have very little clarity of its use.

  2. Its a Useful example. One suggestion: You can provide the sample project as a downloadable file after the post [ as seen on various other blogs eg: Ray Wnderlich ]

  3. You made me save a lot of time, thank you so much! In my project I am using multiple fetched result controllers, segmented table views and showing activity indicator during fetch operation. I tried to do fetch under background thread but it causes crashes in core data. I didn’t think  that “performSelector” will solve it but it works great.

Leave a Reply

Your email address will not be published.


*