Lots of problems with your code. First of all, Your current code will lock up the user interface until the download is complete. That is bad, even for short downloads. If there is a network problem, this can lock up the user interface for up to 2 minutes, even for a short file.
You should download the file using an async download, not synchronous.
Second of all, UI updates only get rendered to the screen when your code returns and the app visits the event loop. Code using this approach:
- Set label text to "doing stuff"
- Do something time-consuming
- Change label text to "done with stuff"
Does not work. The text "doing stuff" never shows up in the label, because the label changes aren't drawn until your code finishes and returns, and by then, you've replaced the label text with "done with stuff."
Here's what you should do instead:
Use the NSURLConnection method sendAsynchronousRequest:queue:completionHandler:
. Put the code that you want to run once the download is complete in the completionHandler block.
That method handles the background stuff for you, and then runs your completion block on the main thread once the download is complete. It's a slick system method.
It should be possible to do the unzipping from the background using dispatch_async. (I'm not familiar with the SSZipArchive library, so I'm not positive that it's thread-safe, but it should be.)
The basic idea is:
- Display a "downloading" message.
- Create an NSURLRequest.
- Use
sendAsynchronousRequest:queue:completionHandler:
to submit the request asynchronously. - In the completion handler:
- Change the message to "unzipping"
- Save the downloaded data to disk if the unzip library requires that it be saved to a file. If the zip library can do the unzipping from memory, you can skip this step.
- Use dispatch_async to unzip the file from the default priority global queue
- At the end of the unzip block, use dispatch_async(dispatch_get_main_queue()) to change the label to "unzip complete" or whatever you want to say. (You have to send the message to the main queue because you can't change the UI from a background thread.)
Try and figure out how to code the above approach. If you get stuck, post what you've tried as an edit to your post and we can guide you, but it's better if you try to write this code yourself. You'll learn that way rather than copy/pasting.