Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does UIActivityIndicator require manual threading on iPhone

I am running creating an iPhone application which performs a costly operation and I wanted to create an activityIndicator to let the user know the application has not frozen.

The operation is performed entirely in one event call... so there is no chance for the UI framework to receive control to actually display and animate this indicator.

The sample apps which use the UIActivityIndicator (or any other similar animation) start and stop the animation in different events, triggered separately at different stages of the program.

Do I need to manually create a separate thread to run my operation in, or is there already default support for this kind of behavior?

like image 687
Akusete Avatar asked Jan 13 '09 23:01

Akusete


3 Answers

I just found someone asking a very similar question with a good answer on the apple iPhone forums. https://devforums.apple.com/message/24220#24220 #Note: you have to have an appleID to view it.

Essentially, yes, you do need to start your process running in a different thread, but its quite easy to do (as paraphrased from user 'eskimo1')

- (IBAction)syncOnThreadAction:(id)sender
{
    [self willStartJob];

    id myObject = [MyObjectClass createNewObject];
    [self performSelectorInBackground:
        @selector(inThreadStartDoJob:)
        withObject:myObject
    ];
}

- (void)inThreadStartDoJob:(id)theJobToDo
{
    NSAutoreleasePool * pool;
    NSString *          status;

    pool = [[NSAutoreleasePool alloc] init];
    assert(pool != nil);

    status = [theJobToDo countGrainsOfSandOnBeach];

    [self performSelectorOnMainThread:
        @selector(didStopJobWithStatus:)
        withObject:status
        waitUntilDone:NO
    ];

    [pool drain];
}

where -willStartJob and -didStopJobWithStatus are my own methods that:

  • disable the UI, to prevent the user from kicking off two jobs at once
  • set up the activity indicator
like image 107
Akusete Avatar answered Nov 05 '22 06:11

Akusete


Yes, you need to put your operation in a separate thread while the UIActivityIndicatorView calls remain on the main thread.

Here's why: If you start animating your indicator view then immediately begin your process, your process will block until finished, and the user will never see any "activity."

Instead, start animating the indicator, then pop a new thread for your process. Use notifications, a delegate pattern, or performSelectorOnMainThread:withObject:waitUntilDone: to let the main thread know that the process is finished. Then stop the animation and continue.

This will ensure that the user knows something is happening while your process does its thing.

like image 32
August Avatar answered Nov 05 '22 07:11

August


Starting a new thread can be overkilling and a source of complexity if you want to do things that are supposed to start on the main thread.

In my own code, I need to start a MailComposer by pushing a button but it can take some time to appear and I want to make sure the UIActivityIndicator is spinning meanwhile.

This is what I do :

-(void)submit_Clicked:(id)event
{
    [self.spinner startAnimating];
    [self performSelector:@selector(displayComposerSheet) 
               withObject:nil afterDelay:0];    
}

It will queue displayComposerSheet instead of executing it straight away. Enough for the spinner to start animating !

like image 33
Xav Avatar answered Nov 05 '22 08:11

Xav