I am trying to Update a label for showing the Progress of the File to be downloaded using the AFNetworking Framework. The problem is that when i set the percentage to the label in the setProgressiveDownloadProgressBlock the label is updated only when the download starts and when download completes.
__weak MTCViewController *weakSelf= self;
[_operation setProgressiveDownloadProgressBlock:^(AFDownloadRequestOperation *operation, NSInteger bytesRead, long long totalBytesRead, long long totalBytesExpected, long long totalBytesReadForFile, long long totalBytesExpectedToReadForFile) {
float percent = (float)(totalBytesRead / totalBytesExpectedToReadForFile)*100;;
// [weakSelf updateProgress:percent];
[weakSelf updateText:[NSString stringWithFormat:@"Progress = %f",percent]];
}];
[_operation start];
Plus when i remove the label update code the block seems to be updating correctly
You need to call all UI changes on the main thread. So, calculate the percentage and then dispatch the code that updates the UI from the main thread:
__weak MTCViewController *weakSelf= self;
[_operation setProgressiveDownloadProgressBlock:^(AFDownloadRequestOperation *operation, NSInteger bytesRead, long long totalBytesRead, long long totalBytesExpected, long long totalBytesReadForFile, long long totalBytesExpectedToReadForFile) {
float percent = ((float)totalBytesRead / (float)totalBytesExpectedToReadForFile)*100;
dispatch_async(dispatch_get_main_queue(), ^{
[weakSelf updateText:[NSString stringWithFormat:@"Progress = %f", round(percent)]];
});
}];
[_operation start];
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With