CoreData – Data Mapping with AFIncrementalStore

The AFIncrementalStore is a concrete implementation of the NSIncrementalStore API (see Chris’ blog and NSHipster for more details), which allows you to access remote endpoints, like RESTful “Web Services”, by using the CoreData API.

The project has three simple examples, including how to apply type mapping, in cases when you can’t use the JSON key in your class, like ID or description (as these are ObjC key words).

As shown in the examples, you need to override the attributesForRepresentation:ofEntity:fromResponse method, like:

- (NSDictionary *)attributesForRepresentation:(NSDictionary *)representation
                                     ofEntity:(NSEntityDescription *)entity
                                 fromResponse:(NSHTTPURLResponse *)response
{
    NSMutableDictionary *mutablePropertyValues =
       [[super attributesForRepresentation:representation
                                  ofEntity:entity
                              fromResponse:response] mutableCopy];

    if ([entity.name isEqualToString:@"Task"]) {
        NSString *description =
          [representation valueForKey:@"description"];
        [mutablePropertyValues setValue:description forKey:@"desc"];
    }
    return mutablePropertyValues;
}

The above code basically says that on the Task entity (managed object) the desc property is used to store the result of the description¬†key in the JSON response. However, the above code only works for reading…

In case you want to create or update a new entity, you need to override the representationOfAttributes:ofManagedObject method like below:

- (NSDictionary *)representationOfAttributes:(NSDictionary *)attributes
                             ofManagedObject:(NSManagedObject *)managedObject {

    NSMutableDictionary *mutablePropertyValues =
        [[super representationOfAttributes:attributes
                           ofManagedObject:managedObject] mutableCopy];

    if ([managedObject.entity.name isEqualToString:@"Task"]) {
        NSString *description = [managedObject valueForKey:@"desc"];
        [mutablePropertyValues setValue:description forKey:@"description"];
        [mutablePropertyValues removeObjectForKey:@"desc"];
    }
    return mutablePropertyValues;
}

This code is basically the other way around… it reads the value from the managed object property (here desc), and stashes that on a dictionary, which is used by the underlying HTTP Client, to send the form data to the server. Inside of that dictionary, we are using the description key, that is required by our RESTful web service. To be safe… remove the odd desc field from the dictionary. Otherwise you may receive an error since the server does not like/understand the desc filed in the request….

Once you managed this little oddnes, it’s straightforward. As mentioned before, the ObjC type mapping to/from JSON is a little odd… This (of course) applies to CoreData and it’s managed objects as well.

About these ads

Howdy!

Posted in aerogear

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

Join 32 other followers

%d bloggers like this: