NSURL缓存问题

时间:2013-10-11 14:46:21

标签: objective-c

我遇到登录API问题。第一次调用工作正常,但后续调用被缓存。这导致了一个问题,因为登录/注销功能基本上已经破裂。

我尝试了很多方法,并且正在实施AFNetworking库。

AppDelegate.m

NSURLCache *sharedCache = [[NSURLCache alloc] initWithMemoryCapacity:0
                                                        diskCapacity:0
                                                            diskPath:nil];
[NSURLCache setSharedURLCache:sharedCache];

在我的网络课程中:

 (AFHTTPRequestOperation *)createRequestOperationWithMethod:(NSString *) method andPath:    (NSString *)path andParams:(NSDictionary *)params
{
  GRAPIClient *httpClient = [GRAPIClient sharedClient];
  [httpClient setParameterEncoding:AFFormURLParameterEncoding];
  NSMutableURLRequest *request = [httpClient requestWithMethod:method
                                                 path:path
                                           parameters:params];

  [request setCachePolicy:NSURLRequestReloadIgnoringCacheData]

  AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
[httpClient registerHTTPOperationClass:[AFHTTPRequestOperation class]];
return operation;

}

我甚至试图覆盖AFHTTPClient中生成的请求

在AFHTTPClient.m中:

[request setCachePolicy:NSURLRequestReloadIgnoringCacheData];
[request setTimeoutInterval:2.0];

我的GRAPIClient实现:

@interface GRAPIClient : AFHTTPClient

+ (GRAPIClient *)sharedClient;
+ (BOOL) isInternetReachable;

@end

@implementation GRAPIClient

+ (BOOL) isInternetReachable
{
    return reachable;
}

+ (GRAPIClient *)sharedClient {
    static GRAPIClient *_sharedClient = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        _sharedClient = [[GRAPIClient alloc] initWithBaseURL:[NSURL    URLWithString:kAFAppDotNetAPIBaseURLString]];
});

[_sharedClient setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) {
    if (status == AFNetworkReachabilityStatusReachableViaWWAN ||
        status == AFNetworkReachabilityStatusReachableViaWiFi ) {
        NSLog(@"Reachable on!");
        reachable = YES;
    }
    else
    {
        NSLog(@"Reachable off!");
        reachable = NO;
    }
}];

return _sharedClient;
}

- (id)initWithBaseURL:(NSURL *)url {
    self = [super initWithBaseURL:url];
    if (!self) {
        return nil;
    }

    [self registerHTTPOperationClass:[AFJSONRequestOperation class]];    
// Accept HTTP Header; see http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.1
[self setDefaultHeader:@"Accept" value:@"application/json"];
    return self;
}
@end

我调试了来自服务器的响应,并通过硬编码两个NSURLRequests同时测试到服务器。一个用户A,一个用户B,然后打印两个用户的响应数据。

首次登录时,用户A登录返回了用户A凭据。用户B返回了用户B凭据。第二次登录时,用户A返回用户A凭据,用户B返回用户A凭据。我不知道如何完全禁用缓存。

2 个答案:

答案 0 :(得分:0)

尝试:

[operation setCacheResponseBlock:^NSCachedURLResponse*(NSURLConnection* connection, NSCachedURLResponse* cachedResponse) {
    return nil;
}];

[request setCachePolicy:NSURLRequestReloadIgnoringLocalAndRemoteCacheData];

答案 1 :(得分:0)

SixteenOtto建议的问题是从服务器和AFNetworking自动使用cookie发送的会话。之前我没有考虑过这个,因为我们使用的是基于auth令牌的不安分API,所以会话的cookie没有任何意义。但是,使用Charles检查HTTP响应标头让我看到了这一点。

添加

[request setHTTPShouldHandleCookies:NO];

我的operation生成器解决了这个问题。