4

I have used a few different strategies in the past to provide the best user experience when going through various screens of a REST based iOS app:

  • leverage NSURLCache: Although this works fine for certain use cases, I am more interested in an approach that works like this: "display cache immediately while calling API, then update screen with newer data if necessary". NSURLCache seems to only provide me with options such as read form cache OR read from remote depending on various policies, but not read from cache THEN read from remote.
  • leverage core data: Either through Restkit, AFIncrementalStore or custom framework, the idea here is to get data from core data while the call is being made. Once the call returns, core data is updated with latest data and interface is refreshed. It can work great, but requires a great deal of configuration and maintenance, adds a high level of complexity. It is a good option when requiring offline mode, but if only caching is required, this looks overkill. -

Does anybody has a different approach to this, or has a lightweight library to recommend that provides advanced caching functionality, without requiring Core Data?

JP Hribovsek
  • 6,707
  • 2
  • 21
  • 26

2 Answers2

0

Leverage NSURLCache :You basically want to have memory caching incorporated into a framework. YES it exist and it is called as AFNetworking. Here is the documentation specifying the same. It abstracts the caching mechanism from your code.

You have to design a Data Controller which checks if data is available on Disk (Core Data) if no then makes a network request using AFNetworking (which also includes memory caching).

There is no framework which provides you Disk Caching using Core Data and Memory caching using NSURLcache

Kunal Balani
  • 4,739
  • 4
  • 36
  • 73
  • This doesn't answer the question. AFNetworking is relying on NSURlcache, which was the first strategy I mentioned with its pros and cons, and the second bullet point was to use a core data based solution. I asked if anybody had thought of a better strategy than those two, or combined them in a lightweight library. To the best of my knowledge, AFNetworking doesn't provide any advanced caching mechanisms beside what is already provided through NSURLCache – JP Hribovsek Nov 04 '13 at 20:55
  • 1
    the answer is that there is no such thing and you have to build your own. – Kunal Balani Nov 08 '13 at 06:08
  • I am not sure if you're being funny and deserve a thumb up or if that is your best effort at answer. Regardless, I added an answer to my own question. – JP Hribovsek Nov 08 '13 at 15:05
0

One possible answer to my question, is to leverage NSCoding/NSKeyedArchiver.

A good description of this can be found here http://nshipster.com/nscoding/

Through NSKeyedArchiver, I can persist data without having to set up an entire data model with Core Data. The question becomes how to build an efficient caching mechanism around NSKeyedArchiver, and if a third party library is already doing that. And it looks like an answer is called TMCache https://github.com/tumblr/TMCache

So my intent at this point is to have my classes conform to NSCoding, have some objects persisted through TMCache. I am planning on routing my API calls first to the cache, while updating the cache in the background.

JP Hribovsek
  • 6,707
  • 2
  • 21
  • 26
  • can you please show the code on how to use nscoding in order to show cache data immediately while calling the api and show newer data when it is available – Mohammad Yunus May 11 '19 at 06:07