Search code examples
objective-cmultithreadingupdatesuiprogressview

How to update UIProgressView correctly in objective-C


I've got class LoadViewController and on start i call this:

- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.

dispatch_async(kBgQueue, ^{

    NSData *dataOfURL = [NSData dataWithContentsOfURL:kRSSURL];

    [self performSelectorOnMainThread:@selector(fetchedData:) withObject:dataOfURL waitUntilDone:YES];
});
}

Where kbgQueue is

   #define kBgQueue dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)

On main thread is executed fetchedData:.

- (void)fetchedData:(NSData *)responseData {

XMLParser *parser = [[XMLParser alloc] initWithData:responseData];
parser.delegate = self;

if ([parser parse]) {

    // if parsed.
    if (!data) {

        data = [[NSMutableArray alloc] init];
    }
    else {

        [data removeAllObjects];
    }

    data = [parser getParsedData];

    ViewController *viewController = [self.storyboard instantiateViewControllerWithIdentifier:@"ViewController"];
    viewController.array = data;

    [self presentViewController:viewController animated:YES completion:^{}];
    //[self reloadData];
}
else {

    NSLog(@"False.");
}

}

As you see i use my own XMLParser helper class, where i fetch all data.When one piece of data is downloaded i call [self.delegate passProgress:fetchedItems/20.0f]; by delegate and in LoadViewController is something like this:

-(void)passProgress:(float)progress {

dispatch_async(dispatch_get_main_queue(), ^{
    [self.progressView setProgress:progress];
    NSLog(@"Updated!");
});
}

But the problem is: progressView isn't updated after download one piece of 20 data items. Where is the problem?


Solution

  • I don't know is it correctly but i changed in LoadViewController from perfromOnMainThread:withObject:waitUntilDone: to perfromInBackground:withObject:, but it works.