gpt4 book ai didi

ios - AFNetworking 中 DidReceiveData 的回调

转载 作者:行者123 更新时间:2023-12-01 19:12:43 25 4
gpt4 key购买 nike

我有一个 GET 服务调用,我必须在收到响应时执行某些操作。
AFNetworking 中的委托(delegate)方法实际上是 block ,据我所知,仅在请求完成或完全失败时通知。

稍微深入了解一下 AFNetworking , 我发现 AFURLConnectionOperation获取回调

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

现在,我的问题是,如何在发起请求的类中获取此回调?

附言我厌倦了发送 NSNotification使用 NSNotificationCenter我确实得到了通知。但是,由于多个同时请求被发送到服务器,我无法区分哪个请求的响应是我被通知的。

编辑

我注意到 AFHTTPRequestOperation继承自 AFURLConnectionOpertaion所以我可以实现 connection:didReceiveData NSURLConnectionDelegate的方法但问题又是,我如何从那里发送回调。 :(

最佳答案

这是一个完整的示例,它有效:

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

关于ios - AFNetworking 中 DidReceiveData 的回调,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15378509/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com