2013-07-20 64 views
1

我的應用程序有一個功能可以將圖像上傳到Web服務器。當這種情況發生在後臺時,我設置了一個進度條,以便用戶可以看到上傳狀態。我正在上傳進度條,如下所示。只要沒有其他的HTTP請求,一切都可以正常工作。一旦有另一個請求setUploadProgressBlock停止更新,但setCompletionBlockWithSuccess最終會被調用,所以我的進度條將在30到70之間的某個百分比,但上傳實際上已完成。對於另一個請求,我沒有使用AFNetworking。下面還有一個例子。我試過調用[operation waitUntillFinished]和其他一些我在網上看到的例子,但似乎沒有任何工作。AFNetworking setUploadProgressBlock停止更新

圖片上傳=

NSData *postData= [[NSString stringWithFormat:@"description=%@&location=%@&image=%@",i, l, d] dataUsingEncoding:NSNonLossyASCIIStringEncoding allowLossyConversion:YES]; 

NSURL *url = [NSURL URLWithString: [NSString stringWithFormat:@"%@/upload/create", SERVICE_URL]]; 
AFHTTPClient *httpClient = [[AFHTTPClient alloc] initWithBaseURL:url]; 

NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:url]; 
[theRequest setHTTPMethod:@"POST"]; 
[theRequest setHTTPBody:postData]; 

AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:theRequest]; 
[operation setUploadProgressBlock:^(NSUInteger bytesWritten, long long totalBytesWritten, long long totalBytesExpectedToWrite) { 
    NSLog(@"Sent %lld of %lld bytes", totalBytesWritten, totalBytesExpectedToWrite); 

     float percentDone = ((float)(totalBytesWritten)/(float)(totalBytesExpectedToWrite)); 

     [progressLabel setText:[NSString stringWithFormat:@"%.0f%%", (percentDone * 100)]]; 
     [progressView setProgress:percentDone]; 

}]; 

[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) { 

    NSLog(@"success: %@", operation.responseString); 

} 
    failure:^(AFHTTPRequestOperation *operation, NSError *error) { 
     NSLog(@"error: %@", operation.responseString); 

    } 
]; 
[httpClient enqueueHTTPRequestOperation:operation]; 

所有其他請求=

NSURL *loginURL = [NSURL URLWithString: [NSString stringWithFormat:@"%@/user/add", SERVICE_URL]]; 

     NSData *postData= [[NSString stringWithFormat:@"user_id=%@", user_id] dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES]; 

     NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:loginURL]; 
     [theRequest setHTTPMethod:@"POST"]; 
     [theRequest setHTTPBody:postData]; 

     NSOperationQueue *queue = [[NSOperationQueue alloc] init]; 
     [NSURLConnection sendAsynchronousRequest:theRequest queue:queue completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) 
     { 


     }]; 

UPDATE:

要解釋一下,這裏是控制檯將是什麼樣子的例子通過這個過程 -

發送的769069個字節

另一個API調用發送

進度條停止得到更新

另一個API呼叫響應

成功437568:圖片上傳響應

回答

0

setText:setProgress從所謂一個錯誤的線程,並沒有得到更新是可能發生在這裏的最不有害的事情。

二者必選其一

dispatch_async(dispatch_get_main_queue(),^{ 
    [progressLabel setText:[NSString stringWithFormat:@"%.0f%%", (percentDone * 100)]]; 
    [progressView setProgress:percentDone]; 
}); 

[progressLabel performSelectorOnMainThread:@selector(setText:) withObject:[NSString stringWithFormat:@"%.0f%%", (percentDone * 100)] waitUntilDone:NO]; 
[progressView performSelectorOnMainThread:@selector(setProgress:) withObject:(id)percentDone waitUntilDone:NO]; 
+0

我在發佈問題之前試過了。根本沒有幫助。進度條更新正常,直到調用另一個請求。如果沒有其他API調用,則進度條將達到100%。 – Sean

1

您的無主HTTP客戶端實例可能會在操作完成前解除分配。使用示例中顯示的靜態單例模式,或將其存儲在某個擁有視圖控制器的strong屬性中。