Reputation: 5357
I have a method which checks for bluetooth devices ([service scanForSensBoxes]).
The method scanForSensBoxes has a timeout of 5 seconds. During this time I would like to animate an Activity Indicator.
There for I start the spinning in beginn of the attached method, and stop it after the call of "scanForSensBoxes".
Apparently this doesn't work. The result of the code I have at the moment is, that the start and stop animation of the spinner is only processed when the "buttonScanPressed" method is completed instead of during the processing of it. Means the spinner never does animate.
How do I have to change the approach to that, so the spinner will animate during the call of "buttonScanPressed"?
Thanks for any help.
- (IBAction)buttonScanPressed:(id)sender {
[_waitingSpinner startAnimating];
SensBoxServiceLib *service = [[SensBoxServiceLib alloc]init];
NSString *tmp=[NSString stringWithFormat:@"%d",[service scanForSensBoxes]];
[_waitingSpinner stopAnimating];
_sensBoxCount.text=tmp;
}
Upvotes: 0
Views: 483
Reputation: 1217
If scanForSensBoxes
is blocking the main thread, the activity indicator won't animate. Assuming this is this case you need to perform the blocking activities within that method on a background queue.
-(IBAction)buttonScanPressed:(id)sender {
// animate activity indicator
[_waitingSpinner startAnimating];
// perform blocking activity in background
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0) {
SensBoxServiceLib *service = [[SensBoxServiceLib alloc] init];
NSString *tmp = [NSString stringWithFormat:@"%d", [service scanForSensBoxes]];
// perform UI updates on main thread
dispatch_async(dispatch_get_main_queue(), {
[_waitingSpinner stopAnimating];
});
});
}
Upvotes: 2
Reputation: 8424
You need to hide it
- (IBAction)buttonScanPressed:(id)sender {
_waitingSpinner.hidden = NO;
[_waitingSpinner startAnimating];
SensBoxServiceLib *service = [[SensBoxServiceLib alloc]init];
NSString *tmp=[NSString stringWithFormat:@"%d",[service scanForSensBoxes]];
[_waitingSpinner stopAnimating];
_waitingSpinner.hidden = YES;
_sensBoxCount.text=tmp;
}
Upvotes: 0