Issue with UIActivityIndicator when loading from url

I am trying to load an image from a url and use an activity indicator to show that the file is loading. However, it doesn't work as my indicator doesn't animate when I call this load function, can someone 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];
          }
     }

      

0


a source to share


2 answers


You need to put [startAnimating indicator] and [stopAnimating indicator]; in separate methods. I believe that the animation doesn't fire until the method reaches its end. So if you split this into multiple methods, this should work

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


Another option is firmware for this. More details here

0


a source


The animation runs in an event loop, which is in the same thread as your code. That is, the animation is not triggered while your code is running.



Instead, you need to either forget about animations, switch to using asynchronous loading methods, or do the loading in a separate thread. I would recommend the async option.

0


a source







All Articles