gpt4 book ai didi

ios - AFNetworking 使用 enqueueBatchOfHTTPRequestOperations 重试失败的操作

转载 作者:塔克拉玛干 更新时间:2023-11-02 10:12:14 24 4
gpt4 key购买 nike

我正在尝试使用异步操作请求,但有时操作请求因请求超时而失败。我怎样才能形成我的 block ,以便在所有操作完成失败或完成但没有超时时重新发送超时操作并执行一些操作。

我真的需要解决这个问题,非常感谢!

[[SDAFParseAPIClient sharedClient] enqueueBatchOfHTTPRequestOperations:pagedOperations progressBlock:^(NSUInteger numberOfCompletedOperations, NSUInteger totalNumberOfOperations) {
NSLog(@"PAGED totalNumberOfOperations: %u numberOfCompletedOperations: %u",totalNumberOfOperations,numberOfCompletedOperations);
} completionBlock:^(NSArray *operations) {

NSMutableArray *retryops = [NSMutableArray array];

for(AFHTTPRequestOperation *operation in operations){
if(operation.error.code == -1001){
NSLog(@"error code %d",operation.error.code);

[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(@"comp");
//actually original completion block is needed
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"fail");
//actually original fail block is needed
}];
[retryops addObject:operation];
//Do something with the response
}else{
//Handle the failure
}
}

if ([retryops count] == 0) {
NSLog(@"all paginated operations completed");
}
else {
[[SDAFParseAPIClient sharedClient] enqueueBatchOfHTTPRequestOperations:retryops progressBlock:
^(NSUInteger numberOfFinishedOperations, NSUInteger totalNumberOfOperations) {

} completionBlock:
^(NSArray *operations) {
NSLog(@"all retry paginated operations completed");
}];
}

最佳答案

我有同样的问题。我一直试图从一个 FTP 站点下载 200 多个文件,而传递给 enqueueBatchOfHTTPRequestOperations 的 NSURLRequests 会在下载第 100 个文件后持续超时。我最终完全放弃了 AFNetworking,因为它不需要并且使解决方案复杂化。这部分是其他几个 SO 答案的混合体。

NSArray *requestsArray = // my array of [[NSMutableURLRequest alloc] initWithURL:url]'s
dispatch_queue_t downloadQueue = dispatch_queue_create("downloadQueue", NULL);
dispatch_async(downloadQueue, ^{
NSURLResponse *response;
NSError *requestError;
for (NSURLRequest *request in requestsArray) {
response = nil;
requestError = nil;
NSData *requestData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&requestError];
if (response == nil) {
// Check for problems
if (requestError != nil) {

}
}
else {
// Data was received.. continue processing
// Make sure to do any GUI related updates in main queue/thread,
// not directly from here
}
}
});

这使用串行调度队列,因此每个同步 NSURLRequest 一次执行一个。当每个请求完成时,我处理来自它的数据,而不是等待所有请求完成。因为它不在主队列中,所以不会阻塞 GUI。

关于ios - AFNetworking 使用 enqueueBatchOfHTTPRequestOperations 重试失败的操作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14399196/

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