2013-05-30 94 views
0

我可以發送圖像到服務器,但我有一個問題。 我可以簡單的塊檢查故障是這樣的:AFNetworking - AFHTTPRequestOperation - 檢查setUploadProgressBlock失敗?

AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request]; 
[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) { 
    NSLog(@"success: %@", operation.responseString); 
} 
failure:^(AFHTTPRequestOperation *operation, NSError *error) { 
    NSLog(@"error: %@", operation.responseString); 
    } 
]; 
[operation start]; 

但我不能看到該塊發送的進度。所以,我發現了正在進行的呼叫回這樣一個特殊的塊:

[operation setUploadProgressBlock:^(NSUInteger bytesWritten, long long totalBytesWritten, long long totalBytesExpectedToWrite) { 
    NSLog(@"Sent %lld of %lld bytes", totalBytesWritten, totalBytesExpectedToWrite); 
}]; 
[httpClient enqueueHTTPRequestOperation:operation]; 

的問題是,與setUploadProgressBlock沒有「失敗」 ......

所以我的問題是...有一個如何檢查發送失敗或沒有?

感謝您的幫助

回答

1

這將是工作的罰款,它會給進展,並進入失效塊,如果事情出了錯:

NSURLRequest *request = [[NSURLRequest alloc] init]; 
AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request]; 
[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) { 
NSLog(@"success: %@", operation.responseString); 
} 
failure:^(AFHTTPRequestOperation *operation, NSError *error) { 
// If access token is null , we ask to the user if he wants to sign in or sign up ???? 
NSLog(@"error: %@", operation.responseString); 
} 
]; 
[operation setUploadProgressBlock:^(NSUInteger bytesWritten, long long totalBytesWritten, long long totalBytesExpectedToWrite) { 
NSLog(@"Sent %lld of %lld bytes", totalBytesWritten, totalBytesExpectedToWrite); 
}]; 
[operation start]; 
+0

它不會做兩次正確的請求??? 因爲我感覺setUploadProgressBlock直接啓動發送... – manonthemoon

+0

不,它只會用[操作啓動]方法觸發一次請求。 – edzio27

+0

好吧,很高興知道:) 感謝您的幫助;) – manonthemoon

0

您可以設置兩個區塊,實現你想要什麼。

相關問題