简体   繁体   中英

What's the use of reachability in AFNetworking?

When in the middle of download connection is lost or there was no connection initially, completionHandler is fired with error and I have no chance to resume after connection restored. What is the proper way to handle resumable downloading with AFNetworking/reachability? Do I have to create another task because this one is already expired due to network failure or there is a way to revive it?

NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];
AFURLSessionManager *man = [[AFURLSessionManager alloc] initWithSessionConfiguration:configuration];

NSURL *URL = [NSURL URLWithString:@"http://my_server.com/video/2.mp4"];
NSURLRequest *request = [NSURLRequest requestWithURL:URL];

NSURLSessionDownloadTask *downloadTask = [man downloadTaskWithRequest:request progress:nil destination:^NSURL *(NSURL *targetPath, NSURLResponse *response) {
    NSURL *documentsDirectoryURL = [[NSFileManager defaultManager] URLForDirectory:NSDocumentDirectory inDomain:NSUserDomainMask appropriateForURL:nil create:NO error:nil];
    return [documentsDirectoryURL URLByAppendingPathComponent:[response suggestedFilename]];
} completionHandler:^(NSURLResponse *response, NSURL *filePath, NSError *error) {
    NSLog(@"File downloaded to: %@, error: %@", filePath, error);
}];

[man.reachabilityManager startMonitoring];
[man.reachabilityManager setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) {
    switch (status) {
        case AFNetworkReachabilityStatusReachableViaWWAN:
        case AFNetworkReachabilityStatusReachableViaWiFi:
            [downloadTask resume];
            break;
        case AFNetworkReachabilityStatusNotReachable:
        default:
            [downloadTask suspend];
            break;
    }
}];

What is the proper way to handle resumable downloading with AFNetworking/reachability? Do I have to create another task because this one is already expired due to network failure or there is a way to revive it?

What you are asking for is how to resume download after a connection failure. You start an NSURLSessionDownloadTask, and during the download the connection fails with an appropriate NSError describing the failure. You want to retry the connection, reusing any previously downloaded data, when the network interface reachability changes.

NSURLSessionDownloadTask can reuse the previously downloaded data if your application grabs the resume data and later passes it to the task that retries the connection. This is documented here and here .

In the case of a network failure, tthe NSError returned will have a userInfo dictionary populated with the NSURLSessionDownloadTaskResumeData key. This will have the data you need to hold on to. When your application attempts to download the data again, the download task should be created using either downloadTaskWithResumeData: or downloadTaskWithResumeData:completionHandler: , passing in the resume data.

In your case, after a network failure that returns an NSError with the NSURLErrorDomain, an appropriate error code, and a userInfo dictionary with a populated NSURLSessionDownloadTaskResumeData key, you would hold on to the NSData value for the NSURLSessionDownloadTaskResumeData key and start monitoring for reachability changes. If the network interface comes back, in the reachability change notification handler you would create a new download task using downloadTaskWithResumeData: or downloadTaskWithResumeData:completionHandler: and pass the NSData you retreived from the NSError's userInfo dictionary.

Take a look to this answer.

AFNetworking + Pause/ Resume downloading big files

Another way to do it is setting the HTTPHeaderField:@"Range" from your NSMutableURLRequest . to set this header use a formatted string that looks like: [NSString stringWithFormat:@"bythes=%lld-", downloadedBytes]

But as you are using AFNetworking instead of NSMutableURLRequest then just follow the instructions in the link I posted.

Worth to mention that if you use NSMutableURLRequest you will have to go to the place where you are writing the file and check the size of it in order to set the header and the server can provide you the remaining of the file from the last downloaded byte.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM