iphone - 进度跟踪问题 - AFNetworking 队列
<p><p>我有以下代码用于使用 AFNetwork 队列下载两个请求</p>
<pre><code>NSMutableArray *operations = ;
NSURLRequest *request1 = ;
AFHTTPRequestOperation *operation1 = [ initWithRequest:request1];
operation1.outputStream = ;
[operation1 setDownloadProgressBlock:^(NSUInteger bytesRead, long long totalBytesRead, long long totalBytesExpectedToRead) {
float progress = (float)totalBytesRead / totalBytesExpectedToRead;
NSLog(@"Progress 1 = %f",progress);
}];
;
NSURLRequest *request2 = ;
AFHTTPRequestOperation *operation2 = [ initWithRequest:request2];
operation2.outputStream = ;
[operation2 setDownloadProgressBlock:^(NSUInteger bytesRead, long long totalBytesRead, long long totalBytesExpectedToRead) {
float progress = (float)totalBytesRead / totalBytesExpectedToRead;
NSLog(@"Progress 2 = %f",progress*100);
}];
;
</code></pre>
<p>向<code>AFTHTTPClient</code></p>添加请求
<pre><code>AFHTTPClient *requestHandler = [ initWithBaseURL:BASEURL];
[requestHandler enqueueBatchOfHTTPRequestOperations:operations progressBlock:^(NSUInteger numberOfCompletedOperations, NSUInteger totalNumberOfOperations) {
NSLog(@"%d completed on %d",numberOfCompletedOperations,totalNumberOfOperations);
} completionBlock:^(NSArray *operations) {
}];
</code></pre>
<p>问题是我为每个请求单独获取进度。但我需要两个请求的整体进展。</p></p>
<br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
<p><p>只需创建一个全局 float 来控制这个,所有进度的总和是:</p>
<p>TotalProgress = firstProgress/numOperations + secondProgress/numOperations</p>
<pre><code>//Declare this like a global.
#define numOperation 2
float globalProgress = 0.0f;
//Your Operation Instances:
NSMutableArray *operations = ;
NSURLRequest *request1 = ;
AFHTTPRequestOperation *operation1 = [ initWithRequest:request1];
operation1.outputStream = ;
[operation1 setDownloadProgressBlock:^(NSUInteger bytesRead, long long totalBytesRead, long long totalBytesExpectedToRead) {
globalProgress += ((float)totalBytesRead / totalBytesExpectedToRead)/numOperation;
}];
;
NSURLRequest *request2 = ;
AFHTTPRequestOperation *operation2 = [ initWithRequest:request2];
operation2.outputStream = ;
[operation2 setDownloadProgressBlock:^(NSUInteger bytesRead, long long totalBytesRead, long long totalBytesExpectedToRead) {
globalProgress += ((float)totalBytesRead / totalBytesExpectedToRead)/numOperation;
}];
;
</code></pre>
<p>希望有帮助:)</p></p>
<p style="font-size: 20px;">关于iphone - 进度跟踪问题 - AFNetworking 队列,我们在Stack Overflow上找到一个类似的问题:
<a href="https://stackoverflow.com/questions/19051331/" rel="noreferrer noopener nofollow" style="color: red;">
https://stackoverflow.com/questions/19051331/
</a>
</p>
页:
[1]