I play with some JSON parsing in iOS, and I wanted to add a counter, this works fine, but I can't stop the counter, and I would like to understand why.
[spinner stopAnimating] is called in the async block, and I believe that the problem is maybe because I can not call the method on the spinner in the block? I registered spinner obj, and the output is:
<UIActivityIndicatorView: 0x76905f0; frame = (150 230; 20 20); layer = <CALayer: 0x768ffb0>>
Maybe someone can make me understand how to deal with these asynchronous methods in objective-c. I am an absolute beginner.
Code:
- (void)viewDidLoad{ [super viewDidLoad]; UIActivityIndicatorView *spinner = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray]; spinner.center = CGPointMake(160, 240); [self.view addSubview:spinner]; [spinner startAnimating]; dispatch_async(kBgQueue, ^{ NSData* data = [NSData dataWithContentsOfURL: kLatestKivaLoansURL]; [self performSelectorOnMainThread:@selector(fetchedData:) withObject:data waitUntilDone:YES]; NSLog(@"loans: %@", spinner); [spinner stopAnimating]; }); }
source share