views:

68

answers:

2

I am writing an application where we communicate with a web service in json. I would love to cache data from this service, so that the user always can show cached data while we are fetching updated data.

I have looked into a minor project; Core Resource that is a layer over Core Data. It converts from JSON to data objects, and it looks very promising. Since this project does not have a large community or high progression in further development, I am not sure if it is smart to use for me.

Do anyone know about a similar project? Or should I make it my self with just an JSON library and Core Data?

A: 

An idea could be to use the URL-cache of NSURLConnection of the regular iPhone URL Loading System which supports caching on URL level. This will work especially in context of RESTFUL webservices. You can use the API's in-build cache or implement you own chaching strategy. (see here) If you don't want to persist your cache, you wouldn't need CoreData.

Chris
I want to persist my objects so the user have all previous loaded data available in off-line mode. I have been looking at a Google class GDataHTTPFetcher which gives brilliant support for memory cache with support for last-modified in the HTTP header. I thought of write something similar with disk cache for iPhone, but then I need to process my data even when the server returns a 304 message. I really liked that class, but it is not what I am looking for.
Andi
right the suggested approach was just meant for non-persistant use. In that case CoreData is the perfect choice
Chris
A: 

It is trivial to convert JSON to Core Data and backwards. I actually posted an answer here on Stack Overflow that walks through that.

JSON and Core Data on the iPhone

Other than that, using NSURLConnection to push and pull the data is all you really need. A "library" for this seems like an extra unneeded complexity.

Marcus S. Zarra
I will go for this solution, and I think you are right.
Andi
I tried your implementation and I do not agree that it is not necessary with a framework/library for the mapping between JSON and Core Data objects. It is possible that I have misunderstood something. I have done a lot of work to make your implementation match my requirements. Your implementation does not care if objects already exists, and should be deleted, altered or created. I think I do a lot of logic that could be used generally. Your code is a good starting point, but is far from what I needed.
Andi
The code that I posted in an answer as an example is just that, an example. It is not a drop in solution to your specific needs. It is a demonstration of the power of KVC to make it easy to do a recursive transformation between JSON and Core Data. A Framework/Library is useless because your data is unique to your application and a generic solution is almost certainly going to be over-complicated and/or inefficient. Write your own implementation based on examples and you will learn much more about the language.
Marcus S. Zarra