gpt4 book ai didi

iphone - 在 iOS 中清除内存警告的 url 缓存

转载 作者:塔克拉玛干 更新时间:2023-11-02 07:49:50 27 4
gpt4 key购买 nike

当收到内存警告时,清除共享的 NSURLCache 是个好习惯吗?像这样:

- (void)applicationDidReceiveMemoryWarning:(UIApplication *)application {
[[NSURLCache sharedURLCache] removeAllCachedResponses];
}

我假设这也会不必要地清除磁盘缓存是否正确?

如果是这样,是否可以只清除内存缓存?

最佳答案

当出现内存警告时,您只需要清除内存缓存而不是磁盘缓存。 removeAllCachedResponses 的问题在于它会清除两者。从我的测试来看,这似乎只清除了内存缓存。

- (void)applicationDidReceiveMemoryWarning:(UIApplication *)application {

NSURLCache * const urlCache = [NSURLCache sharedURLCache];
const NSUInteger memoryCapacity = urlCache.memoryCapacity;
urlCache.memoryCapacity = 0;
urlCache.memoryCapacity = memoryCapacity;
}

我唯一关心的是线程问题。 this底部有脚注文章。

There are a lot of recommendations on StackOverflow about purging the NSURLCache by recreating it, however, we’ve seen this lead to occasional crashes when requests occur on another thread while the cache is being recreated. Our advice is therefore to create the cache once when the app starts and purge it when appropriate.

上述解决方案不会重新创建缓存,但它可能仍然会遇到同样的问题,我还没有对此进行广泛的测试。

关于iphone - 在 iOS 中清除内存警告的 url 缓存,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13583423/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com