Using NSURLCache to quickly display data, refresh in the background

I want my iOS app (at least some endpoints) to have the following web behavior:

  • Always use cache when available, regardless of age (draw UI right away)
  • If the data is outdated, also make a network request (the UI has stale data during this period, but probably pretty close).
  • If the network data comes back, refresh the cache and make the necessary UI updates.

I prefer this behavior because I can set up my caching policy very quickly (long caching times). For data that is rarely updated, this results in a quick return of the UI in general and to the model layer that is being updated essentially in the background (from the user's perspective).

I read about NSURLCache , but I don't see the cache policy, or even a combination of the two, which I'm sure.

Parameters:

  • Use ReturnCacheDataDontLoad

    to always get cache. If a crash or old cache is used ReloadIgnoringLocalCacheData

    to fetch HTTP. (need to check yourself? can you check age?)
  • Use ReturnCacheDataDontLoad

    to always get cache. Then use UseProtocolCachePolicy

    when the cache time is set to very low, and ignore the response if it comes back from the cache (can I find out if it comes back from the cache? This says it is not reliable)
  • Separate the two issues. Use ReturnCacheDataDontLoad

    for all user initiated requests and immediately disable network request if cache is missing. Separately, a worker who keeps track of saved models, updating them in the background when they seem old.
  • Extend NSURLCache
  • Use something OTS that already does this? (- AFNetworking just uses NSURLSession caching . + EVURLCache forces disk caching but expects data to be loaded when the app is installed.
+3


source to share





All Articles