AFNetworking - 如何建立请求在超时的情况下,重试?(AFNetworking - H

2019-06-27 16:03发布

我最近从ASIHTTPRequest迁移到AFNetworking,这一直是很大的。 然而,我与连接服务器有一些问题,有时会导致我的请求超时。 当使用ASIHTTPRequest它使用以下选择是可能的设置上在超时的情况下的请求的重试计数

-setNumberOfTimesToRetryOnTimeout:

这可以在这个帖子得到进一步参考, 可以在ASIHTTPRequest重试?

如果您不熟悉这AFNetworking https://github.com/AFNetworking/AFNetworking#readme

我无法找到AFNetworking等效的API,有没有人发现,超时使用AFNetworking事件重试网络请求的解决方案?

Answer 1:

AFNetworking的马特·汤普森开发商还跟回答这个对我来说。 下面是GitHub的链接解释的解决方案。

https://github.com/AFNetworking/AFNetworking/issues/393

基本上,AFNetworking不支持此功能。 它留给了开发者对个案基础上实现如下图所示(马特 - 汤普森在github上回答)

- (void)downloadFileRetryingNumberOfTimes:(NSUInteger)ntimes 
                              success:(void (^)(id responseObject))success 
                              failure:(void (^)(NSError *error))failure
{
    if (ntimes <= 0) {
        if (failure) {
            NSError *error = ...;
            failure(error);
        }
    } else {
        [self getPath:@"/path/to/file" parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
            if (success) {
                success(...);
            }
        } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
            [self downloadFileRetryingNumberOfTimes:ntimes - 1 success:success failure:failure];
        }];
    }
}


Answer 2:

我在ApiClient类实现私有方法:

- (void)sendRequest:(NSURLRequest *)request successBlock:(void (^)(AFHTTPRequestOperation *operation, id responseObject))successBlock failureBlock:(void (^)(AFHTTPRequestOperation *operation, NSError *error))failureBlock
{
    __block NSUInteger numberOfRetries = 3;
    __block __weak void (^weakSendRequestBlock)(void);
    void (^sendRequestBlock)(void);
    weakSendRequestBlock = sendRequestBlock = ^{
        __strong typeof (weakSendRequestBlock)strongSendRequestBlock = weakSendRequestBlock;
        numberOfRetries--;

        AFHTTPRequestOperation *operation = [self.httpManager HTTPRequestOperationWithRequest:request success:successBlock failure:^(AFHTTPRequestOperation *operation, NSError *error) {
            NSInteger statusCode = [[[error userInfo] objectForKey:AFNetworkingOperationFailingURLResponseErrorKey] statusCode];

            if (numberOfRetries > 0 && (statusCode == 500 || statusCode == 502 || statusCode == 503 || statusCode == 0)) {
                dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 2 * NSEC_PER_SEC), dispatch_get_main_queue(), ^{
                    strongSendRequestBlock();
                });
            } else {
                if (failureBlock) {
                    failureBlock(operation, error);
                }
            }
        }];

        [self.httpManager.operationQueue addOperation:operation];
    };

    sendRequestBlock();
}

使用示例:

- (void)getSomeDetails:(DictionaryResultBlock)block
    {
        if (!block) {
            return;
        }

        NSString *urlString = @"your url string";
        NSMutableURLRequest *request = [self.httpManager.requestSerializer requestWithMethod:@"POST" URLString:[[NSURL URLWithString:urlString relativeToURL:self.defaultUrl] absoluteString] parameters:nil error:nil];

        // Configure you request here
       [request setValue:version forHTTPHeaderField:@"client-version"];

        NSMutableDictionary *bodyParams = @{};   
        [request setHTTPBody:[NSJSONSerialization dataWithJSONObject:bodyParams options:0 error:nil]];

        [self sendRequest:request successBlock:^(AFHTTPRequestOperation *operation, id responseObject) {
            id response = [NSJSONSerialization JSONObjectWithData:responseObject options:0 error:nil];
            block(response, nil);
        } failureBlock:^(AFHTTPRequestOperation *operation, NSError *error) {
            block(nil, error);
        }];
    }


Answer 3:

就我而言,我经常需要重试功能,所以我想出了这个重试策略类,这将有助于你与机智AFNetworking + RetryPolicy

对于AFNetworking 3.0它可以更好地服务。



Answer 4:

根据您的回答,您可以通过使用块以作为参数块做一些更通用的(和棘手的):

typedef void (^CallbackBlock)(NSError* error, NSObject* response);
- (void) performBlock:(void (^)(CallbackBlock callback)) blockToExecute retryingNumberOfTimes:(NSUInteger)ntimes onCompletion:(void (^)(NSError* error, NSObject* response)) onCompletion {
    blockToExecute(^(NSError* error, NSObject* response){
        if (error == nil) {
            onCompletion(nil, response);
        } else {
            if (ntimes <= 0) {
                if (onCompletion) {
                    onCompletion(error, nil);
                }
            } else {
                [self performBlock:blockToExecute retryingNumberOfTimes:(ntimes - 1) onCompletion:onCompletion];
            }
        };
    });
}

然后围绕类似下面的异步HTTP请求:

[self performBlock:^(CallbackBlock callback) {

        [...]
        AFHTTPRequestOperationManager *manager = [WSManager getHTTPRequestOperationManager];
        [manager POST:base parameters:parameters success:^(AFHTTPRequestOperation *operation, id responseObject) {

            dispatch_async(dispatch_get_main_queue(), ^(void){
                    if (callback) {
                        callback(nil, responseObject);
                    }
                });

            } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
                if (callback) {
                    NSError* errorCode = [[NSError alloc] initWithDomain:AppErrorDomain code:[operation.response statusCode] userInfo:@{ NSLocalizedDescriptionKey :error.localizedDescription}];
                    callback(errorCode, nil);
                }
            }];

    } retryingNumberOfTimes:5 onCompletion:^(NSError *error,  NSObject* response) {
        //everything done
    }];

通过这种方式,重试等待HTTP请求完成,你不必实现每个请求方法重试循环。



文章来源: AFNetworking - How to setup requests to be retried in the event of a timeout?