All of the data on my application is pulled through an API via JSON. The nature of a good percentage of this data is that it doesn't change very often. So to go and make JSON requests to get a list of data that doesn't change much didn't seem all that appealing.
I'm looking for the most sensible option to have this JSON saved onto the iPhone in some sort of persistent data store. Obviously one plus of persisting the data would be to provide it when the phone can't access the API.
I've looked at a few examples of having JSON and CoreData interact, for example, but it seems that they only describe transforming NSManagedObjects into JSON. If I can transform JSON into CoreData, my only problem would be being able to change that data when the data from the API does change.
(Or, maybe this is all just silly.)
Therefore, it is also good to subclass NSManagedObject and use generated properties and accessors.
For some conceptual reading, you may want to read documentation about Efficiently Importing Data, especially "Find-or-create". See a previous similar question.
Getting JSON and saving it as Core Data locally is highly reasonable. How I do it is in two stages:
Two good frameworks for converting between JSON and NS* are json-framework and TouchJSON. The below example is based on json-framework.
Let's say you get an array of some objects from JSON. You'd then do:
the cdObjectFromDictionary might be something like this:
The above assumes that the attribute names in your JSON and Core Data model are the same and that the data types match (i.e JSON numbers are Core Data NSNumbers etc). The above code works nicely even if the model changes.
The above code does not consider how to test if the object already exists locally, but you can imagine how to add that. You must have an object ID of some form in your model, and you can see if the object exists locally before adding it, or whether existing object needs to be updated.