4. ASIHTTPRequest-進度追蹤

每一個ASIHTTPRequest有兩個delegate用來追蹤進度: web

  • downloadProgressDelegate 下載)
  • uploadProgressDelegate (上載).

進度delegate能夠是NSProgressIndicators (Mac OS X) 或者 UIProgressViews (iPhone).ASIHTTPRequest會自適應這兩個class的行爲。你也可使用自定義class做爲進度delegate,只要它響應setProgress:函數。 服務器

  • 若是你執行單個request,那麼你須要爲該request設定upload/download進度delegate
  • 若是你在進行多個請求,而且你想要追蹤整個隊列中的進度,你必須使用ASINetworkQueue並設置隊列的進度delegate
  • 若是上述二者你想同時擁有,恭喜你,0.97版之後的ASIHTTPRequest,這個能夠有 ^ ^

IMPORTANT:若是你向一個要求身份驗證的網站上傳數據,那麼每次受權失敗,上傳進度條就會被重置爲上一次的進度值。所以,當與須要受權的web服務器交互時,建議僅當useSessionPersistence爲YES時才使用上傳進度條,而且確保你在追蹤大量數據的上傳進度以前,先使用另外的request來進行受權。  app

追蹤小於128KB的數據上傳進度目前沒法作到,而對於大於128kb的數據,進度delegate不會收到第一個128kb數據塊的進度信息。這是由於CFNetwork庫API的限制。咱們曾向apple提交過bug報告(bug id 6596016),但願apple能修改CFNetwork庫以便實現上述功能。 函數

2009-6-21:Apple的哥們兒們真棒!iPhone 3.0 SDK裏,buffer大小已經被減少到32KB了,咱們的上傳進度條能夠更精確了。 fetch

 

追蹤單個request的下載進度

這個例子中, myProgressIndicator是個 NSProgressIndicator. 網站

ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
[request setDownloadProgressDelegate:myProgressIndicator];
[request startSynchronous];
NSLog(@"Max: %f, Value: %f", [myProgressIndicator maxValue],[myProgressIndicator doubleValue]);

 

追蹤一系列request的下載進度

在這個例子中, myProgressIndicator 是個 UIProgressView, myQueue是個 ASINetworkQueue. url

- (void)fetchThisURLFiveTimes:(NSURL *)url
{
   [myQueue cancelAllOperations];
   [myQueue setDownloadProgressDelegate:myProgressIndicator];
   [myQueue setDelegate:self];
   [myQueue setRequestDidFinishSelector:@selector(queueComplete:)];
   int i;
   for (i=0; i<5; i++) {
      ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
      [myQueue addOperation:request];
   }
   [myQueue go];
}
 
- (void)queueComplete:(ASINetworkQueue *)queue
{
   NSLog(@"Value: %f", [myProgressIndicator progress]);
}

這個例子中,咱們已經爲ASINetworkQueues調用過[myQueue go]了。 代理

 

追蹤單個request的上傳進度

在這個例子中, myProgressIndicator 是個 UIProgressView。 code

ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url];
[request setPostValue:@"Ben" forKey:@"first_name"];
[request setPostValue:@"Copsey" forKey:@"last_name"];
[request setUploadProgressDelegate:myProgressIndicator];
[request startSynchronous];
NSLog(@"Value: %f",[myProgressIndicator progress]);

 

追蹤一系列request的上傳進度

這個例子中, myProgressIndicator是個 NSProgressIndicator, myQueue是個ASINetworkQueue. orm

- (void)uploadSomethingFiveTimes:(NSURL *)url
{
   [myQueue cancelAllOperations];
   [myQueue setUploadProgressDelegate:myProgressIndicator];
   [myQueue setDelegate:self];
   [myQueue setRequestDidFinishSelector:@selector(queueComplete:)];
   int i;
   for (i=0; i<5; i++) {
      ASIHTTPRequest *request = [ASIFormDataRequest requestWithURL:url];
      [request setPostBody:[@"Some data" dataUsingEncoding:NSUTF8StringEncoding]];
      [myQueue addOperation:request];
   }
   [myQueue go];
}
 
- (void)queueComplete:(ASINetworkQueue *)queue
{
   NSLog(@"Max: %f, Value: %f", [myProgressIndicator maxValue],[myProgressIndicator doubleValue]);
}

 

精確進度條vs簡單進度條

ASIHTTPRequest提供兩種進度條顯示,簡單進度條和精確進度條,使用ASIHTTPRequests 和ASINetworkQueues的showAccurateProgress 來控制。爲一個request設置showAccurateProgress只會對該request有效。若是你爲一個隊列設置showAccurateProgress,那麼會影響隊列裏全部的request。

簡單進度條

當使用簡單進度條時,進度條只會在一個request完成時才更新。對於單個request,這意味着你只有兩個進度狀態:0%和100%。對於一個有5個request的隊列來講,有五個狀態:0%,25%,50%,75%,100%,每一個request完成時,進度條增加一次。

簡單進度條(showAccurateProgress = NO)是ASINetworkQueue的默認值,適用於大量小數據請求。

精確進度條

當使用精確進度條時,每當字節被上傳或下載時,進度條都會更新。它適用於上傳/下載大塊數據的請求,而且會更好的顯示已經發送/接收的數據量。

使用精確進度條追蹤上傳會輕微下降界面效率,由於進度delegate(通常是UIProgressViews 或NSProgressIndicators)會更頻繁地重繪。

使用精確進度條追蹤下載會更影響界面效率,由於隊列會先爲每一個GET型request進行HEAD請求,以便統計總下載量。強烈推薦對下載大文件的隊列使用精確進度條,可是要避免對大量小數據請求使用精確進度條。

精確進度條(showAccurateProgress = YES)是以同步方式執行的ASIHTTPRequest的默認值。

 

自定義進度追蹤

ASIProgressDelegate 協議定義了全部能更新一個request進度的方法。多數狀況下,設置你的uploadProgressDelegate或者 downloadProgressDelegate爲 NSProgressIndicator或者UIProgressView會很好。可是,若是你想進行更復雜的追蹤,你的進度delegate實現下列函數要比 setProgress: (iOS) 或者 setDoubleValue: / setMaxValue: (Mac)好:

這些函數容許你在實際量的數據被上傳或下載時更新進度,而非簡單方法的0到1之間的數字。

downloadProgressDelegates方法

  • request:didReceiveBytes: 每次request下載了更多數據時,這個函數會被調用(注意,這個函數與通常的代理實現的 request:didReceiveData:函數不一樣)。
  • request:incrementDownloadSizeBy: 當下載的大小發生改變時,這個函數會被調用,傳入的參數是你須要增長的大小。這一般發生在request收到響應頭而且找到下載大小時。

uploadProgressDelegates方法

  • request:didSendBytes: 每次request能夠發送更多數據時,這個函數會被調用。注意:當一個request須要消除上傳進度時(一般是該request發送了一段數據,可是由於受權失敗或者其餘什麼緣由致使這段數據須要重發)這個函數會被傳入一個小於零的數字。
相關文章
相關標籤/搜索