简体   繁体   中英

xcode - Re call xml from server with “activityIndicator”

I'm using xCode 4.3.2. In this project, When i call 'reloadXMLdata' with button click, its not showing the activityIndicator, by the time of loading it looks like 'hanged', after few seconds it filling the data. How can i show activityIndicator by the time of loading? Could you please help me for fixing this?

-(void)loadXML
{
    NSString *urlAddress = [NSString stringWithFormat:@"my_xml_url"];    
    NSURL *url = [NSURL URLWithString:urlAddress];
    NSXMLParser *xmlParser = [[NSXMLParser alloc] initWithContentsOfURL:url];
    [xmlParser setDelegate:self];
    BOOL success = [xmlParser parse];

    if(success)
    {
        [self.activityIndicator setHidden:TRUE];
        [self.activityIndicator stopAnimating];
        [dataTable reloadData];
    }
    else
    NSLog(@"Error!!!");
}


-(IBAction)reloadXMLdata:(id) sender
{
    [self.activityIndicator setHidden:FALSE];
    [self.activityIndicator startAnimating];
    [self loadXML];
}

The problem is that you block the UI when calling loadXML and the activity indicator doesn't have the opportunity to be displayed. So try using GCD like this:

-(IBAction)reloadXMLdata:(id) sender
{
    [self.activityIndicator setHidden:FALSE];
    [self.activityIndicator startAnimating];
    [self loadXML];
}

-(void)loadXML
{
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        NSString *urlAddress = [NSString stringWithFormat:@"my_xml_url"];    
        NSURL *url = [NSURL URLWithString:urlAddress];
        NSXMLParser *xmlParser = [[NSXMLParser alloc] initWithContentsOfURL:url];
        [xmlParser setDelegate:self];
        BOOL success = [xmlParser parse];

        if(success) {
            dispatch_async(dispatch_get_main_queue(), ^{
                [self.activityIndicator setHidden:TRUE];
                [self.activityIndicator stopAnimating];
                [dataTable reloadData];
            });
        } else {
            NSLog(@"Error!!!");
        }
    });
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM