What is the use of reachability in AFNetworking?

If the connection is lost in the middle of the download, or there was no connection initially, the Handler termination starts with an error and I have no chance to resume after the connection is restored. What is the correct way to handle resumable downloads with AFNetworking / reachability? Should I create another task because it has already expired due to a network failure or is there 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;
    }
}];

      

+3


source to share


2 answers


What is the correct way to handle resumable downloads with AFNetworking / reachability? Should I create another task because it has already expired due to a network failure or is there a way to revive it?

What you are asking is to resume download after connection failure. You run NSURLSessionDownloadTask and during download the connection fails with a corresponding NSError describing the failure. You want to reconnect, reusing previously downloaded data when the availability of the network interface changes.

NSURLSessionDownloadTask can reuse previously downloaded data if your app grabs the resume data and then passes it on to a task that retries the connection. Here, here and here .



On network failure, tthe returned NSError will have the userInfo dictionary populated with the key NSURLSessionDownloadTaskResumeData

. This will have data that you need to hold onto. When your app tries to load data again, the load task should be created using downloadTaskWithResumeData:

or downloadTaskWithResumeData:completionHandler:

, passing in the resume data.

In your case, after a network outage that returns an NSError using NSURLErrorDomain, the corresponding error code, and the key populated userInfo dictionary NSURLSessionDownloadTaskResumeData

, you will stick with the NSData value for the key NSURLSessionDownloadTaskResumeData

and start monitoring reachability changes. If the network interface returns, in the accessibility change notification handler, you must create a new load task with downloadTaskWithResumeData:

or downloadTaskWithResumeData:completionHandler:

and pass the NSData that you got from the NSError user information dictionary.

+4


source


Take a look at this answer.

AFNetworking + Pause / Resume large file downloads

Another way to do this is to install HTTPHeaderField:@"Range"

from NSMutableURLRequest

. to set this header use a formatted string that looks like this:[NSString stringWithFormat:@"bythes=%lld-", downloadedBytes]



But since you are using AFNetworking instead of NSMutableURLRequest, just follow the instructions in the posted link.

It's worth mentioning that if you are using NSMutableURLRequest, you will have to go to where you write the file and check its size to set the header, and the server can provide you with the leftover file from the last byte loaded.

-2


source







All Articles