Question

I have the code below displaying an ActivityIndicator before calling a GCD process. The background process throws a Notification when it's complete or encounters an error. I am calling the stopAnimating method in the error handler but the spinner keeps spinning. Why?

UIActivityIndicatorView *mIndicator;

@interface VC_Main ()
@end

- (void)viewDidLoad
{
   [super viewDidLoad];
   [NSLog(@"viewDidLoad");

   // create indicator for download activity
   mIndicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
   [mIndicator setCenter:CGPointMake([self getScreen].x /2.0, [self getScreen].y / 2.0)]; // landscape mode
   [self.view addSubview:mIndicator]; 

   // fire off a single interval
   [NSTimer scheduledTimerWithTimeInterval:0.0
                                    target:self
                                  selector:@selector(timerTask:)
                                  userInfo:nil
                                   repeats:NO];

...
}


- (void) timerTask:(NSTimer *) timer
{
   NSLog(@"DEBUG: timertask timeout");

   [mIndicator startAnimating];
   ... 
}




// if there is an error parsing xml downloaded from server, it notifies here
- (void) xmlError:(NSNotification *)note
{
   NSLog(@"error parsing xml");
   [mIndicator stopAnimating];  // this doesn't work

   // fire off a refresh using retry timeout
   [NSTimer scheduledTimerWithTimeInterval:TIMEOUT_RETRY_MINS
                                                target:self
                                              selector:@selector(timerTask:)
                                              userInfo:nil
                                               repeats:NO];

   NSLog(@"will retry in %d", TIMEOUT_RETRY_MINS);   
}
Was it helpful?

Solution

As with every UIKit call, you need to do it on the main thread.

Just do:

dispatch_async(dispatch_get_main_queue(), ^{
    [mIndicator stopAnimating];
});

and it should work

OTHER TIPS

Perhaps you retry too soon?

scheduledTimerWithTimeInterval:TIMEOUT_RETRY_MINS

It's supposed to be seconds, not minutes.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top