我正在解决一个问题,我必须在队列中下载大约10个不同的大文件,我需要显示一个进度条,指示总传输的状态。我在iOS4中使用ASIHTTPRequest工作得很好,但我正在尝试转换到AFNetworking,因为ASIHTTPRequest在iOS5中存在问题而不再维护。
我知道您可以使用AFHTTPRequestOperation的downloadProgressBlock报告各个请求的进度,但我似乎找不到报告将在同一个NSOperationQueue上执行的多个请求的整体进度的方法。
有什么建议吗?谢谢!
答案 0 :(得分:1)
[operation setUploadProgressBlock:^(NSInteger bytesWritten, NSInteger totalBytesWritten, NSInteger totalBytesExpectedToWrite) {
NSLog(@"Sent %d of %d bytes", totalBytesWritten, totalBytesExpectedToWrite);
}];
操作是AFHTTPRequestOperation
答案 1 :(得分:0)
我会尝试使用子类对UIProgressView进行子类化,该子类跟踪您正在观看的所有不同项目,然后具有将它们的进度一起添加的逻辑。
使用这样的代码:
@implementation customUIProgressView
-(void) updateItem:(int) itemNum ToPercent:(NSNumber *) percentDoneOnItem {
[self.progressQueue itemAtIndexPath:itemNum] = percentDoneOnItem;
[self updateProgress];
}
-(void) updateProgress {
float tempProgress = 0;
for (int i=1; i <= [self.progressQueue count]; i++) {
tempProgress += [[self.progressQueue itemAtIndexPath:itemNum] floatValue];
}
self.progress = tempProgress / [self.progressQueue count];
}
答案 2 :(得分:0)
您可以将AFURLConnectionOperation子类化为2个新属性:(NSInteger)totalBytesSent
和(NSInteger)totalBytesExpectedToSend
。您应该在NSURLConnection回调中设置这些属性,如下所示:
- (void)connection:(NSURLConnection *)__unused connection
didSendBodyData:(NSInteger)bytesWritten
totalBytesWritten:(NSInteger)totalBytesWritten
totalBytesExpectedToWrite:(NSInteger)totalBytesExpectedToWrite
{
[super connection: connection didSendBodyData:bytesWritten totalBytesWritten:totalBytesWritten totalBytesExpectedToWrite:totalBytesExpectedToWrite];
self.totalBytesSent = totalBytesWritten;
self.totalBytesExpectedToSend = totalBytesExpectedToSend;
}
您的uploadProgress块可能如下所示:
……(NSInteger bytesWritten, NSInteger totalBytesWritten, NSInteger totalBytesExpectedToWrite) {
NSInteger queueTotalExpected = 0;
NSInteger queueTotalSent = 0;
for (AFURLConnectionOperation *operation in self.operationQueue) {
queueTotalExpected += operation.totalBytesExpectedToSend;
queueTotalSent += operation.totalBytesSent;
}
self.totalProgress = (double)queueTotalSent/(double)queueTotalExpected;
}];