Jason Zhao
Jason Zhao

Reputation: 1288

AFNetworking monitor all download processes

I am trying to download files from remote, and I now can monitor every single files download success status

[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {

But any way to monitor the whole process of downloading? How should I know all downloads are finished?

And I tried start download request with

[afhttpClient enqueueBatchOfHTTPRequestOperations:operationArray 
                                              progressBlock:progressBlock 
                                            completionBlock:completionBlock];

seems not work, so what the difference between above code and start download request with [operation start] in a loop?

@mattt (if you can see this)

Upvotes: 0

Views: 996

Answers (3)

Tad Bumcrot
Tad Bumcrot

Reputation: 351

If what you want is a constant progress update for each operation with bytes downloaded and total expected then I can highly recommend Peter Steinberger's AFDownloadRequestOperation.

This class derives from AFHTTPRequestOperation and adds a progressiveDownloadProgressBlock per-operation rather than just a per-operation-completion progress at the operation queue level, which is what I think you're looking for. Another great bonus is that it makes resumable/partial downloads much more accessible than in the core AFNetworking implementation.

it's this easy to use (example from the GitHub project's README.md):

[pdfRequest setProgressiveDownloadProgressBlock:^(NSInteger bytesRead,
            long long totalBytesRead, long long totalBytesExpected,
            long long totalBytesReadForFile, long long totalBytesExpectedToReadForFile)
{
    self.downloadProgress = totalBytesReadForFile/(float)totalBytesExpectedToReadForFile;
}];

I use this in a few enterprise iOS projects to download multi-gigabyte files and I can tell you that it works great with the 1.0.1 release of AFNetworking.

Hope that helps…

Upvotes: 0

Forrest
Forrest

Reputation: 128003

You can create NSOperationQueue and put all AFHTTPRequestOperation into it. To observe the "operations" by using KVO. When the count go to zero, that is the time to say all operations completed.

Upvotes: 0

Eugene
Eugene

Reputation: 10045

You most probably have an NSArray of URL objects that you use to download images. What you need to do is to create an integer value equal to the count of your URL objects. Each time you successfully download an image or absolutely fail to download it (for instance after few timeouts or upon receiving 404 HTTP status code) you need to decrement that integer (note that it should be an atomic property, since blocks are being executed on different threads). Once the count reaches zero - all requests are finished. You can also use that integer to update a progress bar or simply notify user that "#/15 images are downloaded". Let me know if you need any other clarifications.

And unfortunately I have not worked with AFHTTPClient, so I can't tell you the difference between the two operations precisely, but contextually, first one executes all the requests almost at the same time asynchronously and the latter one uses consecutive approach, where second request will only be launched upon completion of the first one.

Upvotes: 2

Related Questions