简体   繁体   English

如何清除/刷新NSString缓存

[英]How to purge / flush cache of NSString

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. 目前,我正在对我的应用程序(针对Mac OS X用xCode编写)进行简单测试,我注意到从互联网获取数据时存在一些问题。 So I am requesting some text data: 所以我要一些文本数据:

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

Now: 现在:

  1. If internet works then everything is awesome. 如果互联网有效,那么一切都很棒。

  2. If internet disconnected then I am getting error in "error" however "dataFromInternet" still returns the very same data as if there was internet connect 如果互联网断开连接,那么我将在“错误”中遇到错误,但是“ dataFromInternet”仍然返回与存在互联网连接相同的数据

  3. 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. 请说明这种(#2&#3)奇怪的行为以及解决方法。 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. 为了从Internet上获取数据而不是将所有问题发布在问题中,您必须使用其他对象。

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

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. 您从Internet上获取数据的方式与使用NSString几乎相同,不同之处在于您指定了以下内容:“ options:NSUncachedRead”-表示它不会缓存数据,并且始终在Internet正常的情况下读取最新的数据。

Once you obtained data you can convert it into NSString if desirable. 获得数据后,可以根据需要将其转换为NSString。 I needed NSString so I converted it back to what I want. 我需要NSString,所以我将其转换回了我想要的。 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. 我可以在Mac上关闭Airport,并且不会接收到任何数据,而一旦打开Airport,数据就会再次流动。 Very simple and works great for me. 非常简单,对我来说很棒。

Thank you. 谢谢。

So I'm not able to repro this. 因此,我无法对此进行复制。 With this code: 使用此代码:

NSError *error = nil;
NSStringEncoding encoding = 12345678; // known bad value
NSString *test = [[NSString alloc] initWithContentsOfURL:[NSURL URLWithString:@"http://www.example.com/non-existant-page.html"] usedEncoding:&encoding error:&error];
if (test == nil) {
    NSLog(@"didnt work:%@, enc=%d, error:%@",test, encoding, error);
} else {
    NSLog(@"worked:%@, enc=%d, error:%@", test, encoding, error);
}

... and without internet, I get this: ...而且没有互联网,我得到了:

2011-08-28 22:30:45.482 test[48578:207] didnt work:(null), enc=12345678, error:Error Domain=NSCocoaErrorDomain Code=256 "The operation couldn’t be completed. (Cocoa error 256.)" UserInfo=0x5b09280 {NSURL=http://www.example.com/non-existant-page.html}

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. 使用 Internet进行确认之后,我还运行了此操作以确认它没有被缓存(不是),所以我不知道您怎么能得到结果。 Can you give us more of the code that you used? 您能给我们更多您使用的代码吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM