Is there "performance loss" using dataWithContentsOfURL:
not there is not. What you lose by using dataWithContentsOfURL:
is a lot of functionality such as:
- 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)
dataWithContentsOfURL:
is suppose to be used to load local files per the documentation. From the docs:
Important: Do not use this synchronous method to request network-based
URLs. For network-based URLs, this method can block the current thread
for tens of seconds on a slow network, resulting in a poor user
experience, and in iOS, may cause your app to be terminated.
What you should use is (from docs):
Instead, for non-file URLs, consider using the
dataTaskWithURL:completionHandler:
method of the NSSession class.
This is how you would use it:
-(void) sendHTTPGet
{
NSURLSessionConfiguration *defaultConfigObject = [NSURLSessionConfiguration defaultSessionConfiguration];
NSURLSession *defaultSession = [NSURLSession sessionWithConfiguration: defaultConfigObject delegate: self delegateQueue: [NSOperationQueue mainQueue]];
NSURL * url = [NSURL URLWithString:@"http://theURL"];
NSURLSessionDataTask * dataTask = [defaultSession dataTaskWithURL:url
completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
if(error == nil)
{
//use your data
}
}];
[dataTask resume];
}