使用AFNetworking的缓存在ios中不起作用

时间:2016-05-07 09:27:18

标签: ios caching afnetworking

我的目标是即使没有互联网连接也能显示数据。可能我错过了如何缓存它的概念。但到目前为止,我已经将 AFHTTPRequestOperationManager 子类化了。

@implementation MYHTTPRequestOperationManager

- (AFHTTPRequestOperation *)HTTPRequestOperationWithRequest:(NSURLRequest *)request
                                                    success:(void (^)(AFHTTPRequestOperation *operation,
                                                                      id responseObject))success
                                                    failure:(void (^)(AFHTTPRequestOperation *operation,
                                                                      NSError *error))failure
{
    NSMutableURLRequest *modifiedRequest = request.mutableCopy;
    AFNetworkReachabilityManager *reachability = self.reachabilityManager;
    if (!reachability.isReachable)
    {
        modifiedRequest.cachePolicy = NSURLRequestReturnCacheDataElseLoad;
    }
    return [super HTTPRequestOperationWithRequest:modifiedRequest
                                          success:success
                                          failure:failure];
}

@end

getRequestWithUrl 方法下的ApiAccess课程中:

- (void) getRequestWithUrl:(NSString*) url params:(NSDictionary*) params tag:(NSString*) tag
{
     MYHTTPRequestOperationManager *manager = [MYHTTPRequestOperationManager manager];
    [manager.requestSerializer setCachePolicy:NSURLRequestReloadIgnoringLocalCacheData];
    [manager GET:[NSString stringWithFormat:@"%@%@",baseurl,url] parameters:params success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSLog(@"JSON: %@", responseObject);

        NSError* error = nil;
        Response *response = [[Response alloc] initWithDictionary:responseObject error:&error];

        if (response.responseStat.isLogin) {
            [self.delegate receivedResponse:responseObject tag:tag index:0];
        }
        else
        {
            [JSONHTTPClient postJSONFromURLWithString:[NSString stringWithFormat:@"%@app/login/authenticate/accesstoken",baseurl] bodyString:[NSString stringWithFormat:@"access_token=%@",accessToken]
                                           completion:^(NSDictionary *json, JSONModelError *err)
             {

                 NSError* error = nil;
                 AccessTokenResponse *response = [[AccessTokenResponse alloc] initWithDictionary:json error:&error];

                 if(response.responseStat.status)
                 {
                     [self postRequestWithUrl:url params:params tag:tag];
                 }

             }];

        }



    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"Error: %@", error);

        //[self.delegate receivedError:error tag:tag];
    }];


}

互联网出现故障时没有任何事情发生。 我该怎么做才能让它发挥作用?

0 个答案:

没有答案