I am using Olivier Poitrey SDURLCache (github link) as an alternative NSURLCacheto enable disk caching in an iPhone application.
It works very well, but there is a curious leak NSHTTPURLResponseInternalwhen the disk cache object is returned (there is no leak when the object is returned from memory or the object was not found). The following code snippet shows how SDURLCache reads data from disk:
- (NSCachedURLResponse *)cachedResponseForRequest:(NSURLRequest *)request
{
NSCachedURLResponse *memoryResponse = [super cachedResponseForRequest:request];
if (memoryResponse)
{
return memoryResponse;
}
NSString *cacheKey = [SDURLCache cacheKeyForURL:request.URL];
NSMutableDictionary *accesses = [self.diskCacheInfo objectForKey:kSDURLCacheInfoAccessesKey];
if ([accesses objectForKey:cacheKey])
{
NSCachedURLResponse *diskResponse = [NSKeyedUnarchiver unarchiveObjectWithFile:[diskCachePath stringByAppendingPathComponent:cacheKey]];
if (diskResponse)
{
[accesses setObject:[NSDate date] forKey:cacheKey];
diskCacheInfoDirty = YES;
[super storeCachedResponse:diskResponse forRequest:request];
return diskResponse;
}
}
return nil;
}
The stack trace for each tag NSHTTPURLResponseInternalcontains two code references SDURLCache.
The first points to a string [accesses setObject:[NSDate date] forKey:cacheKey];. The second and last indicates the following:
@implementation NSCachedURLResponse(NSCoder)
- (id)initWithCoder:(NSCoder *)coder
{
return [self initWithResponse:[coder decodeObjectForKey:@"response"]
data:[coder decodeDataObject]
userInfo:[coder decodeObjectForKey:@"userInfo"]
storagePolicy:[coder decodeIntForKey:@"storagePolicy"]];
}
@end
- ? ? , .
.
: Tweet Olivier Poitrey,
NSURLCache ,