Reputation: 115
I wrote small test app, that doesn't work. This is my storyboard:
(source: cs618122.vk.me)
This is my code:
- (void)viewDidLoad
{
[super viewDidLoad];
UIActivityIndicatorView* ai = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
UIBarButtonItem* rightButton = [[UIBarButtonItem alloc] initWithCustomView:ai];
self.navigationItem.rightBarButtonItem = rightButton;
[ai startAnimating];
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0), ^{
[NSThread sleepForTimeInterval: 5];
NSLog(@"STOP!");
[ai stopAnimating];
});
}
This is work result:
(source: cs618122.vk.me)
What's wrong?
Upvotes: 0
Views: 211
Reputation: 1025
Here is kind of the same problem solved:
[spinner startAnimating];
dispatch_async(kBgQueue, ^{
// ... do other stuff in the background
dispatch_async(dispatch_get_main_queue(), ^{
[spinner stopAnimating];
});
});
UIActivityIndicatorView won't stop
Upvotes: 3
Reputation: 1946
Did you try?
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ [ai stopAnimating]; });
Upvotes: 2
Reputation: 119031
All UI updates must be run on the main thread, and you're running the stop on a background thread.
Also, don't start a background thread and then sleep it. Use a different API to run your delayed function, like a timer or dispatch_after
(which you can add to the main queue and solve both problems).
Upvotes: 1
Reputation: 93
All userinterface updates must be run on the main thread (mainQueue):
[[NSOperationQueue mainQueue] addOperationWithBlock:^ {
[ai stopAnimating];
}];
Upvotes: 1