2011-06-12 72 views
4

我目前正在我的網站上使用對我的API(I設置)的異步調用。我正在使用ASIHTTPRequest的setDownloadProgressDelegate和UIProgressView。然而,我不知道如何調用一個選擇器(updateProgress),它將設置一個CGFloat'進度'到progressView的進度。我嘗試了以下內容,但兩項進展均爲零。請你能告訴我怎樣才能使這個工作?如何使用ASIHTTPRequest(ASYNC)跟蹤下載進度

(in some method) 

ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:[NSURL URLWithString:[url stringByAppendingFormat:@"confidential"]]]; 
    [request setDownloadProgressDelegate:progressView]; 
    [NSTimer scheduledTimerWithTimeInterval:1.0f/60.0f target:self selector:@selector(updateProgress:) userInfo:nil repeats:YES]; 
    [request setCompletionBlock:^{~100 lines of code}]; 
    [request setFailedBlock:^{~2 lines of code :) }]; 
    [request startAsynchronous]; 

- (void) updateProgress:(NSTimer *)timer { 
    if (progressView.progress < 1.0) { 
     currentProgress = progressView.progress; 
     NSLog(@"currProg: %f --- progressViewProg: %f", currentProgress, progressView.progress); 
    } 
    else { 
     [timer invalidate]; 
    } 
    return; 
} 
+0

你能解釋一下你實際上試圖讓代碼做什麼 - 你想有一個進度視圖,也想知道你自己班級的進度嗎? – JosephH 2011-06-12 18:17:08

+0

我希望能夠存儲一個名爲progress的CGFloat,當類中的方法被調用時它將被更新。它將在該方法中由ASIHTTPRequest進行更新。 – 2011-06-12 18:47:22

回答

4

嘗試在添加到您的要求:

[request setShowAccurateProgress:YES]; 

它不會幫你叫updateProgressASIHTTPRequest將改變進度指示器本身。

0

BTW:NS*Connection appears to be quite a bit faster than ASI*當下載的東西。

在任何情況下,example on this page意味着您不需要手動將該值從下載對象「複製」到進度視圖對象。

實際上,基於計時器的代碼正在從應該顯示進度的進度視圖中獲取進度。如果我正確理解ASIHTTP *,那麼在此代碼中就不需要計時器。

+0

btw:有一些比較ASI和NSURLConnection的更新的性能數據在這裏:http://allseeing-i.com/ASIHTTPRequest-1.5 – JosephH 2011-06-12 18:14:28

+1

哦 - 酷 - 看起來像差距縮小。好東西!我仍然傾向於NS *,除非ASI *的額外功能直接導致我的應用程序中的代碼行數量明顯減少。 – bbum 2011-06-12 18:20:45

7

對於仍在尋找這個答案的人來說:請注意ASI是高度不推薦的,您應該使用NSURLSession或ASIHTTPRequest代替。

達到目標的一種方法是將downloadProgressDelegate設置爲您自己的類並實施setProgress:。在此實現,更新進度變量,然後調用[progressView setProgress:];

或者在代碼中,設置請求的下載進度委託:

[request setDownloadProgressDelegate:self]; 

,然後方法添加到您的類:

- (void)setProgress:(float)progress 
{ 
    currentProgress = progress; 
    [progressView setProgress:progress]; 
} 
+0

我怎麼能在我自己的子類中設置委託? – 2011-06-13 16:48:20

+0

我已經添加了一些代碼給我的答案,這有助於解釋它嗎? – JosephH 2011-06-13 17:54:54