如何清除/刷新 NSString 的缓存

发布于 2024-12-01 14:55:43 字数 657 浏览 3 评论 0原文

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

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

现在:

  1. 如果互联网正常,那么一切都很棒。

  2. 如果互联网断开连接,那么我在“错误”中收到错误,但是“dataFromInternet”仍然返回与有互联网连接相同的数据

  3. 如果我在互联网断开连接时请求数据(上面的代码),然后连接互联网并再次请求数据,我仍然会收到错误,就好像互联网无法工作一样!

我不明白这种行为以及发生了什么。我只能猜测有一些缓存机制,但我现在不知道如何修复它。

请解释这个(#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:

  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

  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.
Thank you.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

往事风中埋 2024-12-08 14:55:43

好吧,经过一段时间的互联网漫游并试图找到我的问题的答案后,我得到了以下结果:

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

上面的代码似乎确实使用了缓存。为了从互联网获取数据并且不出现问题中发布的所有问题,您必须使用不同的对象。

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

上面的示例代码中发生了什么?您从互联网获取数据的方式几乎与使用 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:

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

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.

烂柯人 2024-12-08 14:55:43

所以我无法重现这个。使用此代码:

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);
}

...并且没有互联网,我得到了:

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}

我还运行了此之后使用互联网执行以确认它没有被缓存(它不是t),所以我不明白你是如何得到结果的。您能给我们更多您使用的代码吗?

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. Can you give us more of the code that you used?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文