Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does -dataWithContentsOfURL: of NSData work in a background thread?

Tags:

iphone

nsdata

Does -dataWithContentsOfURL: of NSData work in a background thread?

like image 882
dontWatchMyProfile Avatar asked Jun 20 '10 09:06

dontWatchMyProfile


1 Answers

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];
like image 79
Nipuna H Herath Avatar answered Oct 23 '22 10:10

Nipuna H Herath