views:

552

answers:

1

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.)

+5  A: 

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:

  1. convert JSON to native Cocoa data types (NSDictionary and NSArray)
  2. convert NS* into Core Data object

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:

NSArray *objects = [jsonStringYouGotFromServer JSONValue];
for (NSDictionary *_object in objects) {
    CDObjectType *cdObject = [self cdObjectFromDictionary:_object];
    // cdObject is now a full-featured Core Data object
}

the cdObjectFromDictionary might be something like this:

- (CDObjectType *) cdObjectFromDictionary:(NSDictionary *)dict {

    CDObjectType *object = [NSEntityDescription
              insertNewObjectForEntityForName:@"Object"
              inManagedObjectContext:moc];

    NSDictionary *attributes = [[NSEntityDescription
        entityForName:@"Object"
        inManagedObjectContext:moc] attributesByName];

    for (NSString *attr in attributes) {            
        [object setValue:[dict valueForKey:attr] forKey:attr];                            
    }

    return object;
}

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.

Jaanus
Thanks for the insight! This is quite perfect. :)
Bryan Veloso