简体   繁体   中英

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

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.

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?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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