atastrophic
atastrophic

Reputation: 3143

Callback for DidReceiveData in AFNetworking

I have a GET service call for which I must perform certain actions when a response is received.

The delegate methods, blocks actually, in AFNetworking, to my knowledge, only notify when a request is completed or failed wholly.

With a little in-depth look at AFNetworking, I discovered that AFURLConnectionOperation gets the callbacks

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

Now, my question is, how can I get this callback in the class which initiated the request?

P.S. I tired sending an NSNotification using NSNotificationCenter and I did get notified. However, due to multiple simultaneous requests being sent to server, I was unable to distinguish which request's response was I being notified of.

EDIT

I noticed that AFHTTPRequestOperation is inherited from AFURLConnectionOpertaion so I could possibly implement connection:didReceiveData method of NSURLConnectionDelegate but problem again is, how do I send a callback from there. :(

Upvotes: 1

Views: 1138

Answers (1)

Thorsten
Thorsten

Reputation: 3122

Here is a complete example, which works:

    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];

Upvotes: 3

Related Questions