Skip to Content
iOS 5 Programming Cookbook
book

iOS 5 Programming Cookbook

by Vandad Nahavandipoor
February 2012
Intermediate to advanced
872 pages
22h 43m
English
O'Reilly Media, Inc.
Content preview from iOS 5 Programming Cookbook

8.1. Downloading Asynchronously with NSURLConnection

Problem

You want to download a file from a URL, asynchronously.

Solution

Use the NSURLConnection class with an asynchronous request.

Discussion

There are two ways of using the NSURLConnection class. One is asynchronous and the other is synchronous. Asynchronous connection will create a new thread and does its downloading process on the new thread. Synchronous connection will block the calling thread while downloading content and doing its communication.

Many developers think that a synchronous connection blocks the main thread, but that is incorrect. A synchronous connection will always block the thread from which it is fired. If you fire a synchronous connection from the main thread, yes, the main thread will be blocked. But if you fire a synchronous connection from a thread other than the main thread, it will be like an asynchronous connection in that it won’t block your main thread. In fact, the only difference between a synchronous and an asynchronous connection is that the runtime will create a thread for the asynchronous connection, while it won’t do such thing for a synchronous connection.

In order to create an asynchronous connection, we need to:

  1. Have our URL in an instance of NSString.

  2. Convert our string to an instance of NSURL.

  3. Place our URL in a URL Request of type NSURLRequest, or in case of mutable URLs, in an instance of NSMutableURLRequest.

  4. Create an instance of NSURLConnection and pass the URL request to it.

We can create ...

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.

Read now

Unlock full access

More than 5,000 organizations count on O’Reilly

AirBnbBlueOriginElectronic ArtsHomeDepotNasdaqRakutenTata Consultancy Services

QuotationMarkO’Reilly covers everything we've got, with content to help us build a world-class technology community, upgrade the capabilities and competencies of our teams, and improve overall team performance as well as their engagement.
Julian F.
Head of Cybersecurity
QuotationMarkI wanted to learn C and C++, but it didn't click for me until I picked up an O'Reilly book. When I went on the O’Reilly platform, I was astonished to find all the books there, plus live events and sandboxes so you could play around with the technology.
Addison B.
Field Engineer
QuotationMarkI’ve been on the O’Reilly platform for more than eight years. I use a couple of learning platforms, but I'm on O'Reilly more than anybody else. When you're there, you start learning. I'm never disappointed.
Amir M.
Data Platform Tech Lead
QuotationMarkI'm always learning. So when I got on to O'Reilly, I was like a kid in a candy store. There are playlists. There are answers. There's on-demand training. It's worth its weight in gold, in terms of what it allows me to do.
Mark W.
Embedded Software Engineer

You might also like

iOS Programming Cookbook

iOS Programming Cookbook

Hossam Ghareeb
iOS 7 Programming Cookbook

iOS 7 Programming Cookbook

Vandad Nahavandipoor
iOS 6 Programming Cookbook

iOS 6 Programming Cookbook

Vandad Nahavandipoor

Publisher Resources

ISBN: 9781449321123Errata Page