0

NSURLSession has NSURLSessionDataTask and NSURLSessionDownloadTask and I'm more of a fan of data, as I don't need to retrieve it from the file system.

The NSURLSessionDownloadTaskDelegate has a method for retrieving progress on the download, though it doesn't seem the data counterpart does. If I want download progress, am I required to use NSURLSessionDownloadTask?

If so, I understand NSURLSessionDownloadTask stores the file downloaded and then you retrieve it. Does this directory where files are stored need to emptied occasionally, or is it just a temp store that gets removed as soon (or soon after) accessing it?

Doug Smith
  • 29,668
  • 57
  • 204
  • 388

1 Answers1

0

I'd use AFNetworking for that. The AFHTTPRequestOperation allows you to set a block for tracking progress.

AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:YOUR_URL_REQUEST];

[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) 
{
     // Completion code.
} 
failure:^(AFHTTPRequestOperation *operation, NSError *error) 
{      
     // Error handling code.
}];

Here is how you can track progress:

[operation setDownloadProgressBlock:^(NSInteger bytesWritten, long long totalBytesWritten, long long totalBytesExpectedToWrite) 
{
    float progress = ((float)((int)totalBytesWritten) / (float)((int)totalBytesExpectedToWrite));

   // Track the progress here

}];  
Rafał Sroka
  • 39,540
  • 23
  • 113
  • 143