Alik Send
Alik Send

Reputation: 115

iOS: UIActivityIndicator in navigation bar not stopping

I wrote small test app, that doesn't work. This is my storyboard: 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: work result
(source: cs618122.vk.me)

What's wrong?

Upvotes: 0

Views: 211

Answers (4)

Akaino
Akaino

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

avdyushin
avdyushin

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

Wain
Wain

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

heming
heming

Reputation: 93

All userinterface updates must be run on the main thread (mainQueue):

 [[NSOperationQueue mainQueue] addOperationWithBlock:^ {
        [ai stopAnimating];
    }];

Upvotes: 1

Related Questions