3

I'm using NSURLSession to perform an HTTP Post NSMutableURLRequest, using the dataTaskWithRequest:completionHandler: method. When I perform a request for the first time, things take a reasonable amount of time to complete and show some feedback in the completion handler. On the next time that same request is fired, it happens almost instantaneously with very little time in between, which leads me to believe that the system is caching the contents of this data task.

As I don't need to view the returned data, is NSURLSession the best way to do this? It needs to work well with WatchKit, which NSURLSession does, which is why I chose it in the fist place. I would preferably like to find a way to just clear the cache after each request. If need be, I could switch to NSURLConnection, but this would be best. Thanks!

Alex Wulff
  • 2,039
  • 3
  • 18
  • 29

1 Answers1

3

Ephemeral mode will not use any cache.

NSURLSessionConfiguration *ephemeralConfigObject = [NSURLSessionConfiguration ephemeralSessionConfiguration];
Lumialxk
  • 6,239
  • 6
  • 24
  • 47
  • Thanks for the help. Quick question for you: is [session finishTasksAndInvalidate]; necessary or will this happen automatically? From testing the ephemeralSession it looks like the cache is cleared both ways, but what do you think? – Alex Wulff Jan 06 '16 at 01:53
  • @ConiferApps Year,it will happen automatically. If you want to cancel the task immediately,call invalidateAndCancel. – Lumialxk Jan 06 '16 at 02:05
  • Will it hurt anything if I do just for redundancy? – Alex Wulff Jan 06 '16 at 02:06
  • @ConiferApps finishTasksAndInvalidate will wait for the last task. – Lumialxk Jan 06 '16 at 02:07
  • Ok thanks for the help! I'm marking your answer as correct. – Alex Wulff Jan 06 '16 at 02:08
  • I'm not sure this answer is correct for the question, an ephemeral session doesn't have NO cache, just the cache isn't written to disc and is retained in RAM. – Leon Mar 07 '22 at 15:22