I use this code to upload files. The problem is that the loading animation in progrssView done at the last moment. And not during the whole process. How to solve a problem?

I use NSURLSession as advised in the previous question.

 - (void)viewDidLoad { _progressView = [[UIProgressView alloc] initWithProgressViewStyle:UIProgressViewStyleDefault]; _progressView.progressTintColor = [UIColor colorWithRed:0.0/255 green:0.0/255 blue:0.0/255 alpha:0.4]; [[_progressView layer]setFrame:CGRectMake(60, 150, 100, 25)]; [[_progressView layer]setBorderColor:[UIColor whiteColor].CGColor]; _progressView.trackTintColor = [UIColor clearColor]; [_progressView setProgress:(float)(50/100) animated:YES]; [[_progressView layer]setCornerRadius:_progressView.frame.size.width / 8]; [[_progressView layer]setBorderWidth:1]; [[_progressView layer]setMasksToBounds:TRUE]; _progressView.clipsToBounds = YES; [self.view addSubview:_progressView]; _session = [NSURLSession sessionWithConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration] delegate:self delegateQueue:[NSOperationQueue mainQueue]]; [self.progressView setProgress:0 animated:NO]; } 
  • make sure setProgress is called on main thread. And maybe your data just comes at a time (if there are so few of them) - Max Mikheyenko

1 answer 1

Try to update your progress like this:

 dispatch_async(dispatch_get_main_queue(), ^{ //update UI in main thread. [self.progressView setProgress:totalBytesWritten/totalBytesExpectedToWrite animated:YES]; }); } 

And really add a log to see how often you receive data, as advised from above.

  • I tried. Does not work. If I download 50MB, I immediately see that the animation is delayed - User 2:01 pm
  • strange. And you did not try to display the progress in the label, for example? Also try calling dispatch_async in the dispatch_after block (dispatch_time (DISPATCH_TIME_NOW, 0.1 * NSEC_PER_SEC), dispatch_get_main_queue (), ^ {// NSLog (@ "parameter1:% d parameter2:% f", parameter1, dispatch_async) - Valentine
  • label update does not work? - Valentine
  • I did not try to display progress in the label. How should this be done in my case? - User
  • Just print the number of bytes in the label - label.text = [NSString stringWithFormat: @ "% d", totalBytesWritten]; Like that. - Valentine