2013-09-27 2 views
2

나는 문제는 내가 진행되면서 각각에 대해 별도의 AFTHTTPClient문제 - AFNetworking 큐

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

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

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

에 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]; 

추가 요청을 사용하여 두 개의 요청을 다운로드하기위한 다음과 같은 코드를 얻을되어있다 의뢰. 그러나 두 가지 요청을 결합하여 전체적으로 진전이 필요합니다.

답변

1

그냥이를 제어 할 수있는 글로벌 플로트를 만들고, 모든 진보의 총은 다음과 같습니다

TotalProgress = 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]; 

희망이 도움 :)

+0

없음 그것은 잘못된 진전을 가져옵니다. 하지만 코드가 작동하도록 변경합니다. 두 개의 전역 변수를 추가하고 각 진행 상황을 저장하고 실제로 작동합니다. –