iosswiftcachingpersistencenscache

NSCache and persistence?


My app performs searches of some items and downloads an image per each result it gets. It shows the results on a tableView, a cell per result, and each cell shows the corresponding image.

I'd like to cache the images to avoid being downloading the same if a search was already done with the same params. I've been reading about NSCache as a good option for this, for example in this article, but I also read here that NSCache is not persisted between app launches. I couldn't find that information in Apple's documentation though, could somebody help me to find it?

If I'd like to persist the downloaded images even between app launches and I'm not going to be able to do that with NSCache, how should I then manage that?


Solution

  • Caching is an application dependent task. For example, as you say, d cannot use the NPCache because it does not store permanently. You can try to tune your HTTP server(!) such that the URL loading system of your app will cache the downloaded images as long as possible. But it is very hard achieve that the cache does exactly what you want. The third option is to use a free framework which does caching for you. Even though I think they have some flaws, you should take a look at them. The fourth option is, write your own cache that does exactly what you want and need. This is not so complicated as it sounds. As @rmaddy suggested, it is essentially reading and writing an image into the cache folder.