如何清除/刷新 NSString 的缓存
目前,我正在对我的应用程序(用 Mac OS X 的 xCode 编写)进行简单的测试,我注意到从互联网获取数据时存在一些问题。所以我请求一些文本数据:
NSString *dataFromInternet = [[NSString alloc] initWithContentsOfURL:url
usedEncoding:&encoding
error:&error];
现在:
如果互联网正常,那么一切都很棒。
如果互联网断开连接,那么我在“错误”中收到错误,但是“dataFromInternet”仍然返回与有互联网连接相同的数据
如果我在互联网断开连接时请求数据(上面的代码),然后连接互联网并再次请求数据,我仍然会收到错误,就好像互联网无法工作一样!
我不明白这种行为以及发生了什么。我只能猜测有一些缓存机制,但我现在不知道如何修复它。
请解释这个(#2 和#3)奇怪的行为以及如何解决它。 谢谢。
Currently I am doing simple tests of my app (written in xCode for MAC OS X) and I noticed that there are some issues when it comes to getting data from internet. So I am requesting some text data:
NSString *dataFromInternet = [[NSString alloc] initWithContentsOfURL:url
usedEncoding:&encoding
error:&error];
Now:
If internet works then everything is awesome.
If internet disconnected then I am getting error in "error" however "dataFromInternet" still returns the very same data as if there was internet connect
If I request data (above code) while internet disconnected and then connect internet and request data once again, I am still getting error as if internet doesn't work!
I don't understand this behavior and what is going on. I can only guess there is some caching mechanism and I don't now how to fix it.
Please explain this ( #2 & #3 ) odd behavior and how to fix it.
Thank you.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
好吧,经过一段时间的互联网漫游并试图找到我的问题的答案后,我得到了以下结果:
上面的代码似乎确实使用了缓存。为了从互联网获取数据并且不出现问题中发布的所有问题,您必须使用不同的对象。
上面的示例代码中发生了什么?您从互联网获取数据的方式几乎与使用 NSString 相同,除非您指定以下内容:“options:NSUncachedRead” - 意味着它不会缓存数据并始终读取最新和最好的数据 - 在互联网正常工作的情况下。
获得数据后,如果需要,可以将其转换为 NSString。我需要 NSString 所以我将它转换回我想要的。否则原帖中的所有问题都已解决!
我可以关闭 Mac 上的 airport,但不会收到任何数据,一旦我打开 airport,数据就会再次流动。非常简单,对我来说效果很好。
谢谢。
Okay, so after sometime roaming around internet and trying to find answer to my question, here is what I came up with:
Above code does seem to use cache. In order to get data from internet and not to have all issues that are posted in the question, you have to use different object.
What is happening in above sample code? You get data from internet almost the same way as you would with NSString except you specify following: "options:NSUncachedRead" - meaning that it will not cache the data and read always the latest and greatest - under condition that internet works.
Once you obtained data you can convert it into NSString if desirable. I needed NSString so I converted it back to what I want. Otherwise all of issue in original post are solved!
I can turn off airport on my mac and no data will be received and as soon as I turn on airport, data is flowing again. Very simple and works great for me.
Thank you.
所以我无法重现这个。使用此代码:
...并且没有互联网,我得到了:
我还运行了此之后使用互联网执行以确认它没有被缓存(它不是t),所以我不明白你是如何得到结果的。您能给我们更多您使用的代码吗?
So I'm not able to repro this. With this code:
... and without internet, I get this:
I also ran this after doing it with internet to confirm that it wasn't being cached (it isn't), so I don't see how you could have gotten a result. Can you give us more of the code that you used?