Search code examples
iosgrand-central-dispatchnsthreaduiactivityindicatorview

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?


Solution

  • 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