2013-03-13 37 views
1

我有一個GET服務調用,在接收到響應時我必須執行某些操作。AFNetwork中的DidReceiveData回調

據我所知,委託方法實際上在AFNetworking中的塊只在請求完全或失敗時才通知。

隨着一點點的深入瞭解AFNetworking,我發現AFURLConnectionOperation得到回調

- (void)connection:(NSURLConnection __unused *)connection 
didReceiveData:(NSData *)data; 

現在,我的問題是,我怎麼能得到這個回調中,發起請求的類?

P.S.我累了發送NSNotification使用NSNotificationCenter,我確實收到通知。但是,由於多個同時發送請求被髮送到服務器,我無法區分哪個請求的響應是我被通知的。

編輯

我注意到,AFHTTPRequestOperationAFURLConnectionOpertaion繼承所以我可能實現的NSURLConnectionDelegateconnection:didReceiveData方法,但問題又是,我怎麼從那裏發出的回調。 :(

+0

這是否幫助https://github.com/AFNetworking/AFNetworking/wiki/AFNetworking-FAQ - >「如何跟蹤上傳或下載進度?「 – Thorsten 2013-03-13 06:58:41

+0

嗯....我爲我的應用程序使用'AFHttpClient'的實現。我將如何設置這些進度塊? – tGilani 2013-03-13 07:22:28

+0

這樣的事情:NSMutableURLRequest * request = [httpClient requestWithMethod:@」GET「path :@「/ your_path.php」parameters:nil]; AFJSONRequestOperation * operation = [AFJSONRequestOperation JSONRequestOperationWithRequest:request success:blah failure:blah]; [操作setDownloadProgressBlock:^(NSInteger bytesRead,NSInteger totalBytesRead,NSInteger totalBytesExpectedToRead){ myProgressView.progress =(float)totalRead/totalExpected; }; [httpClient enqueueHTTPRequestOperation:operation]; – Thorsten 2013-03-13 07:36:11

回答

3

下面是一個完整的例子,它的工作原理:

NSMutableURLRequest *request = [[HTTPClient sharedInstance] requestWithMethod:@"GET" path:iurl parameters:nil]; 
    AFHTTPRequestOperation *operation = [[HTTPClient sharedInstance] HTTPRequestOperationWithRequest:request success:^(AFHTTPRequestOperation *operation, id responseObject) { 
     NSLog(@"success"); 
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) { 
     NSLog(@"failure"); 
    }]; 
    [operation setDownloadProgressBlock:^(NSUInteger bytesRead , long long totalBytesRead , long long totalBytesExpectedToRead) 
    { 
     NSLog(@"%lld of %lld", totalBytesRead, totalBytesExpectedToRead); 
    } 
    ]; 
    [[HTTPClient sharedInstance] enqueueHTTPRequestOperation:operation]; 
+0

謝謝thorsten!完美的作品! :) – tGilani 2013-03-14 10:12:48

相關問題