Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Activity indicator (spinner) with UIActivityIndicatorView

I have a tableView that loads an XML feed as follows:

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];

    if ([stories count] == 0) {
        NSString *path = @"http://myurl.com/file.xml";
        [self parseXMLFileAtURL:path];
    }
}

I'd like to have the spinner to show on the top bar at the app launch and dissapear once the data is displayed on my tableView.

I thought that putting the beginning at viewDidAppear and the end at -(void)parserDidEndDocument:(NSXMLParser *)parser but it didn't work.

I'd appreciate a good explained solution on how to implement this solution.

like image 588
Cy. Avatar asked Dec 22 '22 05:12

Cy.


1 Answers

Here's the problem: NSXMLParser is a synchronous API. That means that as soon as you call parse on your NSXMLParser, that thread is going to be totally stuck parsing xml, which means no UI updates.

Here's how I usually solve this:

- (void) startThingsUp {
  //put the spinner onto the screen
  //start the spinner animating

  NSString *path = @"http://myurl.com/file.xml";
  [self performSelectorInBackground:@selector(parseXMLFileAtURL:) withObject:path];
}

- (void) parseXMLFileAtURL:(NSString *)path {
  //do stuff
  [xmlParser parse];
  [self performSelectorOnMainThread:@selector(doneParsing) withObject:nil waitUntilDone:NO];
}

- (void) doneParsing {
  //stop the spinner
  //remove it from the screen
}

I've used this method many times, and it works beautifully.

like image 146
Dave DeLong Avatar answered Jan 12 '23 06:01

Dave DeLong