0
votes

I'm trying to find a way to prevent NSURLSession from caching responses (in Simulator) by using approach from these questions:

Prevent NSURLSession from caching responses

NSURLCache does not clear stored responses in iOS8

I don't want to use approach from this question:

Clear NSURLSession Cache

which is using ephemeralSessionConfiguration call to NSURLSessionConfiguration when setting up a new NSURLSession instance.

I just want to not cache requests, in worst case clear the cache when application resigns active. With call [NSURLCache sharedURLCache] removeAllCachedResponses] in applicationWillResignActive execution in app delegate i still get Cache.db present and full of cached responses/requests in respective iOS Simulator cache on disk.

Before that, I changed all my NSURLSessionConfiguration cache policy to NSURLRequestReloadIgnoringCacheData NSURLRequestReloadIgnoringLocalCacheData NSURLRequestReloadIgnoringLocalAndRemoteCacheData (tested all three cases, the last one is probably not implemented) instances (and effectively ALL cache policies). Isn't changing cache policy supposed to result in not caching server responses and requests on simulator? Does it depends on storage policy in response headers?

I'm using iOS 9.3 and iOS 10.1 Simulators for iPhone and v. 10 of iOS SDK. Project is in Objective C, maybe full Swift project would behave differently? Is this behavior different for simulators and with devices? Why is this happening, is there a solution different from using dependency (can't fall onto one right now for some reason).

2
Did you try setting the cachePolicy property on NSMutableURLRequest to NSURLRequestReloadIgnoringCacheData? Also, set URLCache property on NSURLSessionConfiguration object to nil.Kunal Shrivastava

2 Answers

2
votes

I had a similar issue and I was able to fix this by setting the properties- URLCache and requestCachePolicy on NSURLSessionConfiguration to nil and NSURLRequestReloadIgnoringCacheData respectively.

Also, you can try setting the cachePolicy property on NSMutableURLRequest to NSURLRequestReloadIgnoringCacheData.

0
votes

So the rude solution is ... drop the Cache.db file on app resigning. I assume on the simulator you should be able to?!

But, did you try the ephemeralSessionConfiguration? Because Apples header doc says: * An ephemeral session has no persistent disk storage for cookies, * cache or credentials.