gpt4 book ai didi

iphone - 进度跟踪问题 - AFNetworking 队列

转载 作者:塔克拉玛干 更新时间:2023-11-02 21:40:12 24 4
gpt4 key购买 nike

我有以下使用 AFNetwork 队列下载两个请求的代码

NSMutableArray *operations = [NSMutableArray array];
NSURLRequest *request1 = [NSURLRequest requestWithURL:URL1];
AFHTTPRequestOperation *operation1 = [[AFHTTPRequestOperation alloc] initWithRequest:request1];
operation1.outputStream = [NSOutputStream outputStreamToFileAtPath:PATH1 append:NO];

[operation1 setDownloadProgressBlock:^(NSUInteger bytesRead, long long totalBytesRead, long long totalBytesExpectedToRead) {
float progress = (float)totalBytesRead / totalBytesExpectedToRead;
NSLog(@"Progress 1 = %f",progress);
}];
[operations addObject:operation1];

NSURLRequest *request2 = [NSURLRequest requestWithURL:URL2];
AFHTTPRequestOperation *operation2 = [[AFHTTPRequestOperation alloc] initWithRequest:request2];
operation2.outputStream = [NSOutputStream outputStreamToFileAtPath:PATH2 append:NO];

[operation2 setDownloadProgressBlock:^(NSUInteger bytesRead, long long totalBytesRead, long long totalBytesExpectedToRead) {
float progress = (float)totalBytesRead / totalBytesExpectedToRead;
NSLog(@"Progress 2 = %f",progress*100);
}];
[operations addObject:operation2];

AFTHTTPClient 添加请求

AFHTTPClient *requestHandler = [[AFHTTPClient alloc] initWithBaseURL:BASEURL];

[requestHandler enqueueBatchOfHTTPRequestOperations:operations progressBlock:^(NSUInteger numberOfCompletedOperations, NSUInteger totalNumberOfOperations) {
NSLog(@"%d completed on %d",numberOfCompletedOperations,totalNumberOfOperations);

} completionBlock:^(NSArray *operations) {
}];

问题是我为每个请求分别获取了进度。但我需要这两个请求的整体进展。

最佳答案

只需创建一个全局 float 来控制它,所有进度的总和为:

总进度 = firstProgress/numOperations + secondProgress/numOperations

//Declare this like a global.
#define numOperation 2
float globalProgress = 0.0f;



//Your Operation Instances:
NSMutableArray *operations = [NSMutableArray array];
NSURLRequest *request1 = [NSURLRequest requestWithURL:URL1];
AFHTTPRequestOperation *operation1 = [[AFHTTPRequestOperation alloc] initWithRequest:request1];
operation1.outputStream = [NSOutputStream outputStreamToFileAtPath:PATH1 append:NO];

[operation1 setDownloadProgressBlock:^(NSUInteger bytesRead, long long totalBytesRead, long long totalBytesExpectedToRead) {
globalProgress += ((float)totalBytesRead / totalBytesExpectedToRead)/numOperation;
}];
[operations addObject:operation1];

NSURLRequest *request2 = [NSURLRequest requestWithURL:URL2];
AFHTTPRequestOperation *operation2 = [[AFHTTPRequestOperation alloc] initWithRequest:request2];
operation2.outputStream = [NSOutputStream outputStreamToFileAtPath:PATH2 append:NO];

[operation2 setDownloadProgressBlock:^(NSUInteger bytesRead, long long totalBytesRead, long long totalBytesExpectedToRead) {
globalProgress += ((float)totalBytesRead / totalBytesExpectedToRead)/numOperation;
}];
[operations addObject:operation2];

希望这有帮助:)

关于iphone - 进度跟踪问题 - AFNetworking 队列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19051331/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com