Animate Activity Indicator before downloading file

Discussion in 'iOS Programming' started by maxxiejw, Nov 21, 2012.

  1. maxxiejw, Nov 21, 2012
    Last edited by a moderator: Nov 21, 2012

    maxxiejw macrumors newbie

    Joined:
    Nov 21, 2012
    #1
    I'm new to Xcode and am in the middle of creating an app and at the moment it can download and save a pdf file from URL. When clicking on the download button, it stays highlighted until the file is downloaded and then the rest of the code is carried out aka. the indicator appears after I've downloaded the file.
    I've got the activity indicator code in front of my download code and I've put in an if statement to check the indicator is visible before downloading, but no luck.

    Code:
    [_downloadActivity startAnimating]; 
    
    if(_downloadActivity.hidden == FALSE){
    ....... \\download code
    }
    Any help would be great :) Thanks
     
  2. Reason077, Nov 21, 2012
    Last edited by a moderator: Nov 21, 2012

    Reason077 macrumors 68000

    Reason077

    Joined:
    Aug 14, 2007
    #2
    What does the "download code" code look like? It sounds like you are blocking the UI thread while the download is happening, and not giving it a chance to return to the run loop and update the UI.

    But that shouldn't happen if you're using something like NSURLConnection.
     
  3. Duncan C macrumors 6502a

    Duncan C

    Joined:
    Jan 21, 2008
    Location:
    Northern Virginia
    #3
    As the other poster said, you shouldn't be doing synchronous downloading on the main thread. That blocks the UI until the download is complete.

    Much better to create an NSURLConnection and start it downloading asynchronously.

    If you ARE going to do synchronous network activity, you need to return and service the event loop before starting the download. Something like this:


    Code:
    [_downloadActivity startAnimating]; 
    [self performSelector: @selector(doDownloadStuff) 
      withObject: nil 
      afterDelay: 0];
    
    Then put the code that does the downloading in a method doDownloadStuff. Even though the delay value is 0, the system returns to the main event loop, starts your activity indicator running, and THEN calls your method.
     

Share This Page