Activity indicator (spinner) with UIActivityIndicatorView

前端 未结 4 527
耶瑟儿~
耶瑟儿~ 2021-02-04 21:32

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

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

    if ([stories count] == 0) {         


        
4条回答
  •  一整个雨季
    2021-02-04 22:18

    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.

提交回复
热议问题