如何清除/刷新NSString缓存 [英] How to purge / flush cache of NSString

查看:153
本文介绍了如何清除/刷新NSString缓存的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目前,我正在对我的应用程序(针对Mac OS X用xCode编写)进行简单测试,并且我注意到从互联网获取数据时存在一些问题。所以我要一些文本数据:

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];

现在:


  1. 如果Internet正常工作,那么一切都很棒。

  1. If internet works then everything is awesome.

如果Internet断开连接,那么我将收到错误消息错误但是, dataFromInternet仍然返回与存在互联网连接相同的数据

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.

请解释这种(#2和#3)奇怪的行为以及如何要解决这个问题。
谢谢。

Please explain this ( #2 & #3 ) odd behavior and how to fix it. Thank you.

推荐答案

好吧,所以经过一段时间在互联网上漫游并试图找到问题的答案之后,我想出了以下几点:

Okay, so after sometime roaming around internet and trying to find answer to my question, here is what I came up with:

NSString *dataFromInternet = [[NSString alloc] initWithContentsOfURL:url
                                             usedEncoding:&encoding 
                                                    error:&error];

以上代码似乎确实使用了缓存。为了从互联网上获取数据而不是问题中没有发布的所有问题,您必须使用其他对象。

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.

NSData* data = [[NSData alloc] initWithContentsOfURL:url options:NSUncachedRead error:&error];
NSString *dataFromInternet = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];

上面的示例代码中发生了什么?除了指定以下内容外,您从Internet上获取数据的方式与使用NSString几乎相同: options:NSUncachedRead-表示它不会缓存数据,并且始终在Internet正常的情况下读取最新的数据。

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.

一旦获得数据,可以根据需要将其转换为NSString。我需要NSString,所以我将其转换回了我想要的。否则,原始帖子中的所有问题都将得到解决!

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!

我可以在Mac上关闭机场,并且不会收到任何数据,而一旦打开机场,数据就会再次流动。非常简单,对我来说很棒。

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.

谢谢。

这篇关于如何清除/刷新NSString缓存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆