Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6588357
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T17:02:17+00:00 2026-05-25T17:02:17+00:00

NSData has always had a very convenient method called +dataWithContentsOfURL:options:error: . While convenient, it

  • 0

NSData has always had a very convenient method called +dataWithContentsOfURL:options:error:. While convenient, it also blocks execution of the current thread, which meant it was basically useless for production code (Ignoring NSOperation). I used this method so infrequently, I completely forgot that it existed. Until recently.

The way I’ve been grabbing data from the tubes is the standard NSURLConnectionDelegate approach: Write a download class that handles the various NSURLConnectionDelegate methods, gradually build up some data, handle errors, etc. I’ll usually make this generic enough to be reused for as many requests as possible.

Say my typical downloader class runs somewhere in the ballpark of 100 lines. That’s 100 lines to do asynchronously what NSData can do synchronously in one line. For more complexity, that downloader class needs a delegate protocol of its own to communicate completion and errors to its owner, and the owner needs to implement that protocol in some fashion.

Now, enter Grand Central Dispatch, and I can do something as fantastically simple as:

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^(void) {

    NSData* data = [NSData dataWithContentsOfURL:someURL];
    // Process data, also async...

    dispatch_async(dispatch_get_main_queue(), ^(void) {
        // Back to the main thread for UI updates, etc.
    });
});

And I can throw that sucker in anywhere I want, right in-line. No need for a download class, no need to handle connection delegate methods: Easy async data in just a few lines. The disparity between this approach and my pre-GCD approach is of a magnitude great enough to trigger the Too Good to be True Alarm.

Thus, my question: Are there any caveats to using NSData + GCD for simple data download tasks instead of NSURLConnection (Assuming I don’t care about things like download progress)?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T17:02:18+00:00Added an answer on May 25, 2026 at 5:02 pm

    You are losing a lot of functionality here:

    • Can’t follow the download progression
    • Can’t cancel the download
    • Can’t manage the possible authentication process
    • You can’t handle errors easily, which is really important especially in mobile development like on iPhone of course (because you often lose your network in real conditions, so it is very important to track such network error cases when developing for iOS)

    and there’s probably more I guess.


    The right approach for that is to create a class than manages the download.

    See my own OHURLLoader class for example, which is simple and I made the API to be easy to use with blocks:

    NSURL* url = ...
    NSURLRequest* req = [NSURLRequest requestWithURL:url];
    
    OHURLLoader* loader = [OHURLLoader URLLoaderWithRequest:req];
    [loader startRequestWithCompletion:^(NSData* receivedData, NSInteger httpStatusCode) {
        NSLog(@"Download of %@ done (statusCode:%d)",url,httpStatusCode);
        if (httpStatusCode == 200) {
            NSLog(%@"Received string: %@", loader.receivedString); // receivedString is a commodity getter that interpret receivedData using the TextEncoding specified in the HTTP response
        } else {
            NSLog(@"HTTP Status code: %d",httpStatusCode); // Log unexpected status code
        }
    } errorHandler:^(NSError *error) {
        NSLog(@"Error while downloading %@: %@",url,error);
    }];
    

    See the README file and sample project on github for more info.

    This way:

    • you still rely on the asynchronous methods provided by NSURLConnection (and as the Apple’s documentation says about Concurrency Programming if an API already exists to make asynchronous tasks, use it instead of relying on another threading technology if possible)
    • you keep advantages of NSURLConnection (error handlings, etc)
    • but you also have the advantages of the blocks syntax that makes your code more readable than when using delegate methods
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have following code in my application. NSData *data=[NSData dataWithContentsOfURL:[NSURL URLWithString:pathOfThumbNail]]; pathOfThumbNail has following
NSData +dataWithContentsOfURL has any kind of caching by default? Has anyone experimented some kind
NSFileManager has a method to do copying. - (BOOL)copyItemAtPath:(NSString *)srcPath toPath:(NSString *)dstPath error:(NSError **)error
The Situation My custom controller class has the following method: - (void)requestViewControllerWithIdentifier:(NSString *)identifier fromObject:(id)object;
I'm using [NSData dataWithContentsOfURL:] to create two NSData instances and I want to compare
I am trying to load UIImage object from NSData , and the sample code
why can´t I fill my NSArray? Where is my mistake? He always just fill
Whenever I implement a method in my own code that can accept or return
NSData *data = [[NSData alloc] initWithContentsOfURL:url]; Url is an NSURL and it works fine.
imageData = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:srcAddres]]; Downloading jpeg to my imagedata will spend some

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.