我正在使用NSURLSessionDataTask
下载数据。我正在看到连接中断的情况,但我仍然收到数据。我应该如何处理这些案件?
expectedContentLength
进行比较吗?它可靠吗?如果它不可用怎么办?代码:
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
{
// How do I know if data is complete?
}
}];
[task resume];