7

我正在解决一个问题,我必须在队列中下载大约 10 个不同的大文件,并且我需要显示一个进度条来指示总传输的状态。我在 iOS4 中使用 ASIHTTPRequest 可以正常工作,但我正在尝试过渡到 AFNetworking,因为 ASIHTTPRequest 在 iOS5 中存在问题并且不再维护。

我知道您可以使用 AFHTTPRequestOperation 的 downloadProgressBlock 报告单个请求的进度,但我似乎无法找到一种方法来报告将在同一个 NSOperationQueue 上执行的多个请求的整体进度。

有什么建议么?谢谢!

4

3 回答 3

1
[operation setUploadProgressBlock:^(NSInteger bytesWritten, NSInteger totalBytesWritten, NSInteger totalBytesExpectedToWrite) {
    NSLog(@"Sent %d of %d bytes", totalBytesWritten, totalBytesExpectedToWrite);
}];

Operation is AFHTTPRequestOperation

于 2011-12-20T09:29:49.150 回答
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;
}];
于 2011-11-28T04:04:59.817 回答
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];
}
于 2011-11-27T23:10:47.767 回答