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?
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:
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.
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 !
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With