I use NSURLSessionDataTaskto load data. I see cases when the connection is interrupted, and yet I still receive the data. How should I deal with these cases?
- Am I guaranteed to get an error?
- Should I compare the length of the data with the response
expectedContentLength? Is it reliable? What to do if it is unavailable? - Something else?
the code:
NSURLSession *session = [NSURLSession sharedSession];
NSURLSessionDataTask *task = [session dataTaskWithURL:url completionHandler:^(NSData *data, NSURLResponse *response, NSError *error)
{
if (error)
{
NSLog(@"Request %@ failed with error %@", url, error);
return;
}
const long long expectedContentLength = response.expectedContentLength;
if (expectedContentLength > -1)
{
const NSUInteger dataLength = data.length;
if (dataLength < expectedContentLength)
{
NSLog(@"Request %@ received %ld out of %ld bytes", url, (long)dataLength, (long)expectedContentLength);
return;
}
}
else
{
}
}];
[task resume];
source
share