2011-11-08 14 views
7

私はキュー内の10個の大きなファイルをダウンロードしなければならない問題に取り組んでおり、合計転送の状況を示すプログレスバーを表示する必要があります。私はiOS4のASIHTTPRequestでうまく動作していますが、ASIHTTPRequestはiOS5で問題があり、もはや維持されていないため、AFNetworkingに移行しようとしています。ASIHTTPRequestからAFNetworkingに切り替える - ASINetworkQueueの問題

AFHTTPRequestOperationのdownloadProgressBlockを使用して個々のリクエストの進捗状況を報告できますが、同じNSOperationQueueで実行される複数のリクエストの全体的な進捗状況を報告する方法が見つからないようです。

提案がありますか?ありがとう!

答えて

0

私はあなたが見ているすべての異なるアイテムを追跡し、それらのすべての進捗を追加するロジックを持っているサブクラスでUIProgressViewをサブクラス化しようとします。 (NSInteger)totalBytesSent、および(NSInteger)totalBytesExpectedToSend:あなたは2つの新しい特性を有することがAFURLConnectionOperationをサブクラス化することができます

@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]; 
} 
+0

ですデザインパターン。おそらく、 –

+0

。これは動的サイズのコンテンツの仕事に適したツールです。コンテンツのサイズを最初に知る必要なしに、進捗状況を正確に表示します。大きなファイル(ビデオや写真など)のサイズを見つけることは、費用のかかる操作になります。 – clreina

0

:おそらくこのようなコードで

。あなたはそのよう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; 
}]; 
1
[operation setUploadProgressBlock:^(NSInteger bytesWritten, NSInteger totalBytesWritten, NSInteger totalBytesExpectedToWrite) { 
    NSLog(@"Sent %d of %d bytes", totalBytesWritten, totalBytesExpectedToWrite); 
}]; 

操作は、これはMVCに悪いコーディングの練習でAFHTTPRequestOperation

関連する問題