Does -dataWithContentsOfURL: of NSData work in a background thread?
7 Answers
No, it doesn't.
In order to get data from URL asynchronously you should use the NSURLRequest
and NSURLConnection
approach.
You will have to implement the NSURLConnectionDelegate
methods:
-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response;
-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data;
-(void)connectionDidFinishLoading:(NSURLConnection *)connection;
-(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error;

- 33,281
- 23
- 160
- 191

- 14,245
- 13
- 50
- 64
-
1This post shows an example of how to use `+dataWithContentsOfURL:` on a background thread to achieve the same result: http://londonwebdev.com/2011/01/13/retrieving-remote-images-on-background-threads-on-the-iphone/ – user8472 Jul 18 '11 at 13:33
-
Of course! Anything may be done in a background thread, But it wasn't the question. In addition, I would do the same with NSOperationQueue rather than as it is implemented in the post that you have mentioned... – Michael Kessler Jul 19 '11 at 09:26
-
1@user8472, Read the question and the answer once again... The method `dataWithContentsOfURL` is NOT executed in background by itself. Obviously, you can execute anything (almost) in the background thread. You have many possibilities - not only the one I have suggested (which is the parallel solution to `dataWithContentsOfURL`). You could also implement it by `performSelectorInBackground...`, `NSOperation` + `NSOperationQueue`, blocks (as you have mentioned) and few other ways... The bottom line - there is no conflict in my answer. – Michael Kessler Aug 16 '11 at 16:46
-
1It seems the original question is ambiguous - it could be interpreted to read either way. Actually I found my way to this page wondering if it was possible to run it on a background thread (I've now confirmed that it is possible). I've never edited a question - is this the sort of thing we could edit it for - to clarify the ambiguity? This could help future people who come to this question. – bearMountain Apr 20 '12 at 20:10
I'm using dataWithContentsOfURL in a background thread fine.
-(void)loaddata {
NSData* data = [NSData dataWithContentsOfURL:@"some url"];
if (data == nil) {
DLog(@"Could not load data from url: %@", url);
return;
}
}
Call something like this from main thread.
[self performSelectorInBackground:@selector(loaddata) withObject:nil];
If you want to perform updates to ui at end of loaddata, be sure to call a function on main thread.

- 15,955
- 18
- 111
- 232
No. You can use NSURLSession instead, though.
NSURLSessionConfiguration *sessionConfig = [NSURLSessionConfiguration defaultSessionConfiguration];
NSString *imageURL = @"Direct link to your download";
NSURLSession *session = [NSURLSession sessionWithConfiguration:sessionConfig delegate:self delegateQueue:nil];
NSURLSessionDownloadTask *getImageTask = [session downloadTaskWithRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:imageURL]] completionHandler:^(NSURL * _Nullable location, NSURLResponse * _Nullable response, NSError * _Nullable error) {
dispatch_async(dispatch_get_main_queue(), ^{
UIImage *downloadedImage = [UIImage imageWithData:[NSData dataWithContentsOfURL:location]];
});
}];
[getImageTask resume];

- 1,228
- 1
- 16
- 26

- 101
- 1
- 4
No, it blocks the current thread.
You need to use NSURLConnection
in order to have asynchronous requests.

- 161,348
- 33
- 346
- 320
I'm guessing this has changed a bit over the years. But, these days,
NSURLRequest* request = [[NSURLRequest alloc] initWithURL:[NSURL URLWithString:urlString]];
[NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse* response, NSData* data, NSError* error) {
}];
will give you an async network call.

- 4,715
- 5
- 28
- 41
No, this will block the thread and you will load the contents of file into the RAM. You can download content directly into file without temporary NSData to avoid huge RAM usage. Something like this solution https://stackoverflow.com/a/6215458/2937913