bugs:initWithContentsOfURL缓存数据

本文探讨了在使用NSData的initWithContentsOfURL方法时遇到的缓存问题。通过实验观察到,即使在网络断开的情况下,该方法仍能返回之前加载的数据。文章建议避免使用此方法直接获取远程数据,推荐使用NSURLConnection进行替代。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

    今天在项目中发现个问题,就是在用NSData的initWithContentsOfURL获取数据时发现initWithContentsOfURL貌似具有缓存功能。我在一GCD中获取远程数据时利用了initWithContentsOfURL,当我在获取过程中断开网络,该方法返回Error Domain=NSCocoaErrorDomain Code=256 "The operation couldn’t be completed. (Cocoa error 256.)"错误,但是当我再次调用这个函数的时候,却能返回数据(这时候网还是断开的),我就猜测其使用了网络缓存。另外开到该方法 可有一个options,但设置为NSDataReadingUncached并无效果,经查询NSDataReadingUncached主要是针对文件缓存。

    DO NOT USE THESE APIs.在获取远程数据时避免使用NSData方法,而应改为NSURLConnection

http://stackoverflow.com/questions/2149929/how-to-know-when-nsdatas-initwithcontentsofurl-has-finished-loading

http://stackoverflow.com/questions/12102557/on-ios-since-datawithcontentsofurloptionserror-has-an-option-to-not-cache-da

http://www.cocoabuilder.com/archive/cocoa/139147-nsimage-initwithcontentsofurl-vs-nsurlloadresourcedatanotifyingclient.html

http://stackoverflow.com/questions/17832960/is-nsdata-iddatawithcontentsofurlnsurl-aurl-optionsnsdatareadingoption

http://stackoverflow.com/questions/2149929/how-to-know-when-nsdatas-initwithcontentsofurl-has-finished-loading

转载于:https://my.oschina.net/michael1986/blog/212294

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值