views:

3292

answers:

6

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?

A: 

An UIProgressView (the progress bar) can not run by itself, you need to periodically increment its value.

An UIActivityIndicatorView (the spinning gear) can run by itself: just call startAnimating when you start your operation and stopAnimating when you're done.

So an UIActivityIndicatorView may be what you're looking for.

Stephan Burlot
+7  A: 

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.

August
+10  A: 

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];

    [self performSelectorInBackground:
        @selector(inThreadStartDoJob:)
        withObject:theJobToDo
    ];
}

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

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

    status = [... do long running job specified by theJobToDo ...]
    [self performSelectorOnMainThread:
        @selector(didStopJobWithStatus:)
        withObject:status
        waitUntilDone:NO
    ];

    [pool drain];
}

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

o disable the UI, to prevent the user from kicking off two jobs ations at once

o set up the activity indicator
Akusete
A: 

Hi, I am new to iPhone programming, so please correct me if I am wrong. and also forgive me for my silly doubt. I want to know whether iPod/iTouch or iPhone allows background processing / threading?

Pravara
That's not an answer to this question. You should post it as a question on its own.
Peter Hosey
+1  A: 

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 !

Xav
A: 

Which is easier:

1> Have 1 method that starts the spinner (in the background).
2> Run your long-running code right here.
3> Have 1 method that ends the spinner.

or....

A> Have 1 method that starts the spinner.
B> Have 20 methods (1 for each thing you want to do in the background)
C> Have 1 method that ends the spinner.

I always see everyone suggest A,B,C... the hard way.

HelenJ
Can you clarify which threads are being used for each of (1,2,3,A,B,C) steps.
Akusete