【问题标题】:How do I set a request timeout and cache policy in AFNetworking 2.0?如何在 AFNetworking 2.0 中设置请求超时和缓存策略?
【发布时间】:2013-11-26 01:33:44
【问题描述】:

我正在按照给定的示例代码进行操作

AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
[manager GET:@"http://example.com/resources.json" parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
    NSLog(@"JSON: %@", responseObject);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
    NSLog(@"Error: %@", error);
}];

为了更改超时和缓存策略,我“破解”了库并创建了

- (AFHTTPRequestOperation *)GET:(NSString *)URLString
                     parameters:(NSDictionary *)parameters
                          timeoutInterval:(NSTimeInterval)timeoutInterval
                    cachePolicy:(NSURLRequestCachePolicy)cachePolicy
                        success:(void (^)(AFHTTPRequestOperation *operation, id responseObject))success
                        failure:(void (^)(AFHTTPRequestOperation *operation, NSError *error))failure
{
    NSMutableURLRequest *request = [self.requestSerializer requestWithMethod:@"GET" URLString:[[NSURL URLWithString:URLString relativeToURL:self.baseURL] absoluteString] parameters:parameters];
    [request setTimeoutInterval:timeoutInterval];
    [request setCachePolicy:cachePolicy];
    AFHTTPRequestOperation *operation = [self HTTPRequestOperationWithRequest:request success:success failure:failure];
    [self.operationQueue addOperation:operation];

    return operation;
}

有没有一种干净的方法可以做到这一点?

【问题讨论】:

  • 一种让您自己的类扩展 AFHTTPRequestOperationManager 并将方法添加到该类的干净方法。
  • 这确实是一个好点……我会暂时这样做。
  • 没有更清洁的方法。实用方法很简洁,不包含您可能想要设置的所有参数。

标签: ios objective-c afnetworking afnetworking-2


【解决方案1】:

我有点懒得分类或子类化。您可以直接访问管理器的请求序列化程序:

AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
manager.requestSerializer.timeoutInterval = INTERNET_TIMEOUT;
manager.requestSerializer.cachePolicy = NSURLRequestReloadIgnoringLocalAndRemoteCacheData;

【讨论】:

    【解决方案2】:

    最好是创建一个子类

    (也可以同样的方式添加缓存策略)

    TimeoutAFHTTPRequestSerializer.h

    #import "AFURLRequestSerialization.h"
    
    @interface TimeoutAFHTTPRequestSerializer : AFHTTPRequestSerializer
    
    @property (nonatomic, assign) NSTimeInterval timeout;
    
    - (id)initWithTimeout:(NSTimeInterval)timeout;
    
    @end
    

    TimeoutAFHTTPRequestSerializer.m

    #import "TimeoutAFHTTPRequestSerializer.h"
    
    @implementation TimeoutAFHTTPRequestSerializer
    
    - (id)initWithTimeout:(NSTimeInterval)timeout {
    
        self = [super init];
        if (self) {
            self.timeout = timeout;
        }
        return self;
    
    }
    
    - (NSMutableURLRequest *)requestWithMethod:(NSString *)method
                                     URLString:(NSString *)URLString
                                    parameters:(NSDictionary *)parameters
                                         error:(NSError *__autoreleasing *)error
    {
        NSMutableURLRequest *request = [super requestWithMethod:method URLString:URLString parameters:parameters error:error];
    
        if (self.timeout > 0) {
            [request setTimeoutInterval:self.timeout];
        }
        return request;
    }
    
    @end
    

    使用

    self.requestOperationManager.requestSerializer = [[TimeoutAFHTTPRequestSerializer alloc] initWithTimeout:30];
    

    【讨论】:

      【解决方案3】:

      您也可以创建一个类别AFHTTPRequestOperationManager+timeout来添加这个方法,而不必继承AFHTTPRequestOperationManager。

      【讨论】:

        【解决方案4】:

        查看方法 1,了解一种更简洁的方法https://stackoverflow.com/a/21238330/435040

        不同之处在于我使用的是子类化,而不是修补 AFNetworking 的代码。

        有一件事我忘了说。在那个答案中,我只是更改了超时间隔,但添加一些其他缓存策略只是多了 1 行代码。

        【讨论】:

          【解决方案5】:

          尝试类似:

          NSURLRequest *request = [NSURLRequest requestWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:kRequestTimout];
          

          kRequestTimout 是你想要的超时时间

          然后构建您的序列化请求:

          NSURLRequest *serializedRequest = [self.requestOperationManager.requestSerializer requestBySerializingRequest:request withParameters:parameters error:&error];
          

          并创建并添加您的请求操作:

          AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:serializedRequest];
          [operation setCompletionBlockWithSuccess:successBlock failure:failureBlock];
          [self.requestOperationManager.operationQueue addOperation:operation];
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2014-10-05
            相关资源
            最近更新 更多