Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to track progress of multiple simultaneous downloads with AFNetworking?
    text
    copied!<p>I am using AFNetworking to download files that my app uses for a sync solution. At certain times, the app downloads a series of files as a batch unit. <a href="https://stackoverflow.com/a/19883392/353137">Following this example</a>, I run the batch like this:</p> <pre><code>NSURL *baseURL = &lt;NSURL with the base of my server&gt;; AFHTTPRequestOperationManager *manager = [[AFHTTPRequestOperationManager alloc] initWithBaseURL:baseURL]; // as per: https://stackoverflow.com/a/19883392/353137 dispatch_group_t group = dispatch_group_create(); for (NSDictionary *changeSet in changeSets) { dispatch_group_enter(group); AFHTTPRequestOperation *operation = [manager POST:@"download" parameters: &lt;my download parameters&gt; success:^(AFHTTPRequestOperation *operation, id responseObject) { // handle download success... // ... dispatch_group_leave(group); } failure:^(AFHTTPRequestOperation *operation, NSError *error) { // handle failure... // ... dispatch_group_leave(group); }]; [operation start]; } // Here we wait for all the requests to finish dispatch_group_notify(group, dispatch_get_main_queue(), ^{ // run code when all files are downloaded }); </code></pre> <p>This works well for the batch downloads. However, I want to display to the user an MBProgressHUD which shows them how the downloads are coming along.</p> <p>AFNetworking provides a callback method</p> <pre><code>[operation setDownloadProgressBlock:^(NSUInteger bytesRead, long long totalBytesRead, long long totalBytesExpectedToRead) { }]; </code></pre> <p>... which lets you update a progress meter pretty easily, simply by setting the progress to <code>totalBytesRead / totalBytesExpectedToRead</code>. But when you have multiple downloads going simultaneously that is hard to keep track of on a total basis.</p> <p>I have considered having an <code>NSMutableDictionary</code> with a key for each HTTP operation, with this general format:</p> <pre><code>NSMutableArray *downloadProgress = [NSMutableArray arrayWithArray:@{ @"DownloadID1" : @{ @"totalBytesRead" : @0, @"totalBytesExpected" : @100000}, @"DownloadID2" : @{ @"totalBytesRead" : @0, @"totalBytesExpected" : @200000} }]; </code></pre> <p>As each operation's download progresses, I can update the <code>totalBytesRead</code> for that specific operation in the central <code>NSMutableDictionary</code> -- and then total up all the <code>totalBytesRead</code> and <code>totalBytesExpected' to come up with the total for the whole batched operation. However, AFNetworking's progress callback method</code>downloadProgressBlock<code>, defined as</code>^(NSUInteger bytesRead, long long totalBytesRead, long long totalBytesExpectedToRead){}<code>does not include the specific operation as a callback block variable (as opposed to the</code>success<code>and</code>failure` callbacks, which do contain the specific operation as a variable, making it accessible). Which makes it impossible, as far as I can tell, to determine which operation specifically is making the callback.</p> <p>Any suggestions on how to track the progress of multipole simultaneous downloads using AFNetworking?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload