- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
我正在使用 Olivier Poitrey's SDURLCache (github 链接)作为 NSURLCache
的替代方案,以在 iPhone 应用程序中启用磁盘缓存。
它工作得很好,但奇怪的是,当返回磁盘缓存对象时会泄漏 NSHTTPURLResponseInternal
(当从内存返回对象或未找到对象时没有泄漏)。以下代码片段显示了 SDURLCache 如何从磁盘读取数据:
- (NSCachedURLResponse *)cachedResponseForRequest:(NSURLRequest *)request
{
NSCachedURLResponse *memoryResponse = [super cachedResponseForRequest:request];
if (memoryResponse)
{
return memoryResponse;
}
NSString *cacheKey = [SDURLCache cacheKeyForURL:request.URL];
// NOTE: We don't handle expiration here as even staled cache data is necessary for NSURLConnection to handle cache revalidation.
// Staled cache data is also needed for cachePolicies which force the use of the cache.
NSMutableDictionary *accesses = [self.diskCacheInfo objectForKey:kSDURLCacheInfoAccessesKey];
if ([accesses objectForKey:cacheKey]) // OPTI: Check for cache-hit in a in-memory dictionnary before to hit the FS
{
NSCachedURLResponse *diskResponse = [NSKeyedUnarchiver unarchiveObjectWithFile:[diskCachePath stringByAppendingPathComponent:cacheKey]];
if (diskResponse)
{
// OPTI: Log the entry last access time for LRU cache eviction algorithm but don't save the dictionary
// on disk now in order to save IO and time
[accesses setObject:[NSDate date] forKey:cacheKey];
diskCacheInfoDirty = YES;
// OPTI: Store the response to memory cache for potential future requests
[super storeCachedResponse:diskResponse forRequest:request];
return diskResponse;
}
}
return nil;
}
每个 NSHTTPURLResponseInternal
泄漏的堆栈跟踪都包含对 SDURLCache
代码的两个引用。
第一个指向行 [accesses setObject:[NSDate date] forKey:cacheKey];
。第二个也是最新的指向以下内容:
@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 from Olivier Poitrey ,代码作者
I'm planning on removing the NSURLCache inherency and handle the memory cache, it may fix the leak
最佳答案
本身不是答案,而是意识。 iOS 5.0 及更高版本上的 NSURLCache 现在默认缓存到闪存和 RAM。因此,如果您使用 SDURLCache 以获得磁盘缓存,并且目标是 5.0 及更高版本,则没有理由使用 SDURLCache。
关于iphone - 使用 SDURLCache(NSURLCache 的子类)时的内存泄漏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2992704/
我在将 URLResponse 缓存到磁盘时遇到问题。我想要这样,因为我正在下载的数据需要在满足 http header 中的过期标签时重新加载,而不是更早。另外,我希望 iPod touch 用户能
我正在编写一个 iPhone 应用程序,其中一个选项卡是 twitter feed,我正在解析 twitter xml 并将其很好地放入表格 View 中。如果没有互联网连接,我想显示上次有互联网连接
我在使用同步调用缓存 NSURLConnection 响应时遇到问题。我在一个类中初始化缓存,然后在另一个类中使用它。请注意高速缓存容量如何初始化为 100KB,但随后又神奇地重置为零。 - (id)
我在我的应用程序中使用 Alamofire 框架来处理 HTTP 请求。即使标准 URLCaching 未打开,Alamofire 也将其设置为默认值。 Caching Caching is hand
我对 this NSHipster article 有点困惑其中声明您必须在 appDidFinishLaunching 中为您的缓存初始化参数。我认为 NSURLCache 自动成为 NSURLSe
我正在尝试在服务器上存储大量图像并使用 AFNetworking 的图像缓存功能,该功能利用了 NSUrlCache,这样应用程序就不会不需要一直访问服务器,甚至可以离线工作。 重要的是图像永远不会过
我正在使用以下代码从 TfL 下载一些数据。 NSURLSession *session = [NSURLSession sessionWithConfiguration:[NSURLSessionC
Apple 从 iOS 5.0 开始支持磁盘缓存。我以前使用的是自制解决方案,但我正在测试 NSURLCache,希望最终能使用它,因为我在过去看到过奇怪的行为。 我遇到的一个更令人费解的问题是 ca
我正在使用 NSURLCache,我想为 NSURLRequest 设置自定义键。这可能吗? 完整解释: 我正在开发一个 map 应用程序,它使用 OpenStreetMap 瓦片。 OpenStre
我知道 NSCache 是线程安全的,但是我找不到任何文档提到 NSURLCache 是线程安全的。 最佳答案 除非documentation明确地说 NSURLCache 是线程安全的,那么它就不是
我正在尝试保护存储在 NSURLCache 中的敏感数据。我的应用程序文件和核心数据 sqlite 文件设置为 NSFileProtectionComplete。但是,我无法将 NSURLCache
我正在实现一个应用程序,它对我们也控制的 rest-api 进行大量网络调用。最近我们决定在服务器端引入缓存 header 以节省一些宝贵的网络和服务器时间。由于我们事先不知道数据的有效期,我们没有发
所以我对 NSURLCache 进行了子类化,每次我调用 loadHTMLFromString: 它都会调用 storeCachedRequest:forRequest: 然后是 cachedResp
我正在为我的 iOS 应用程序寻找一个跨启动持久的网络缓存解决方案。我开始阅读有关 NSURLCache 的内容,但没有看到任何关于持久性的内容。有谁知道当您使用 NSURLCache 然后关闭并打开
我有一个 NSURLCache,它只记录 cachedResponseForRequest 和 storeCachedResponse 调用。 似乎 UIWebView 会为每个请求调用 cached
我正在尝试弄清楚如何使用 URL 加载框架来利用缓存来加载 URL。 我正在使用 NSURLConnections 并向它们提供 NSURLRequests。我什至将这些请求的缓存策略设置为 NSUR
我正在使用 Cordova/Phonegap 构建一个 iOS 应用程序,它基本上有一个 UIWebView 并从外部资源加载我的网络应用程序。 (iOS6+) 此 Web 应用程序需要 450kb
我正在使用 AFNetworking 和 NSURLCache。请求是使用 NSURLRequestUseProtocolCachePolicy 创建的。应用程序接收带有 header 的响应: Et
This post建议这样做: - (void)applicationDidReceiveMemoryWarning:(UIApplication *)application { [[NSUR
我有一个应用程序,我正在下载很多图像以供稍后显示。 即使没有互联网连接,该应用程序也需要正常运行,因此图像会一次性加载并使用 NSURLCache 持久化。 这是一个巧妙的解决方案,因为我可以使用普通
我是一名优秀的程序员,十分优秀!