How to purge / flush cache of NSString

2019-07-16 07:00发布

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.

2条回答
啃猪蹄的小仙女
2楼-- · 2019-07-16 07:25

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?

查看更多
Anthone
3楼-- · 2019-07-16 07:26

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.

查看更多
登录 后发表回答