Tutorial :UIActivityIndicator issue when downloading from URL



Question:

I'm trying to download the picture from URL and use a activity indicator animating to present the fact that the file is downloading. However, it is not working as my indicator isn't animating when I call this download function, can somebody tell me why?

-(void)download{      [indicator startAnimating];      NSString *downloadPath=@"http://www.xyz.com/path/pic.jpg;       NSData *downloadData=[NSData dataWithContentsOfURL:[ NSURL  URLWithString:downloadPath]];      if(downloadData){             //do something            [indicator stopAnimating];       }        else{          //do something          [indicator stopAnimating];            }       }  


Solution:1

You need to put [indicator startAnimating] and [indicator stopAnimating]; in separate methods. I believe the animation does not kick in until the method reaches its end. So if you separate this into several methods this should work

  • One method that starts your animation
  • One method that downloads the file.
  • One method that stops the animation.

Another option is threading to acomplish this. More information here


Solution:2

The animation is performed in the event loop, which is in the same thread as your code. That is, the animation won't start while your code is executing.

Instead you either need to forget about the animation, switch to using the asynchronous download methods or perform the download in a separate thread. I'd recommend the async option.


Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »