我正在使用AFNetworking V2.5.4。
AFN的默认超时为60秒,这对我的应用来说太长了。我想把它设置为20秒。
所以我尝试按照代码manager.requestSerializer.timeoutInterval = 20;
但它不起作用。我搜索了一些帖子,说我需要添加[manager.requestSerializer willChangeValueForKey:@"timeoutInterval"];
manager.requestSerializer.timeoutInterval = 3;
[manager.requestSerializer didChangeValueForKey:@"timeoutInterval"];
但是,它还没有用。
关注是我的代码
+ (AFHTTPRequestOperation *)sendRequestWithRequest:(NSMutableURLRequest*) request
manager:(AFHTTPRequestOperationManager *)manager
success:(void (^)(AFHTTPRequestOperation *operation, id responseObject))success
failure:(void (^)(AFHTTPRequestOperation *operation, NSError *error))failure{
[manager.requestSerializer willChangeValueForKey:@"timeoutInterval"];
manager.requestSerializer.timeoutInterval = 3;
[manager.requestSerializer didChangeValueForKey:@"timeoutInterval"];
NSLog(@"manager %@, start time %lf",manager, [[NSDate date] timeIntervalSince1970]);
AFHTTPRequestOperation *operation = [manager HTTPRequestOperationWithRequest:request
success:^(AFHTTPRequestOperation *operation, id responseObject) {
if (operation.response.statusCode) {
success(operation, responseObject);
}else{
failure(operation, nil);
}
}
failure:^(AFHTTPRequestOperation *operation, NSError *error) {
failure(operation, error);
}];
[manager.operationQueue addOperation:operation];
return operation;}