NSURLConnection仍然存储缓存数据

时间:2014-04-07 08:10:16

标签: ios nsurlconnection

当我使用NSURLConnection从我的服务器获取数据时,iOS仍会缓存数据。在请求新数据时,iOS向我的服务器发送新请求,但文件仍然存储。

我使用NSURLRequestReloadIgnoringCacheData并在connection:willCacheResponse:中返回nil。

问题在于我使用自己的缓存对象,让iOS存储副本是一种愚蠢的行为。

我应该忽略这一点,还是应该以某种方式删除缓存?

更新

创建NSURLRequest的代码:

request = [NSURLRequest requestWithURL:[NSURL URLWithString:URLString] cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:30.0];

创建NSURLConnection的代码:

NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
[connection start];

NSURlConnectionDelegate的代码:

- (NSCachedURLResponse *)connection:(NSURLConnection *)connection willCacheResponse:(NSCachedURLResponse *)cachedResponse {
    return nil;
}

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
    [recivedData appendData:data];
}

- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
    if (_handler) _handler(response, error, nil);
}

- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
    if (_handler) _handler(response, nil, recivedData);

    recivedData = nil;
    response = nil;
}

0 个答案:

没有答案