Using Mantle with AeroGear

The last two days I looked at Mantle, and how it solves some issues, when working with JSON and a rich domain model. Now, how to use the Mantle framework with AeroGear’s iOS library?

The Model – with Mantle

Ok, here is a quick snippet of a model:

@interface AGTask : MTLModel

@property(strong, nonatomic) NSNumber *recId;
@property(copy, nonatomic) NSString *title;
@property(copy, nonatomic) NSString *description;

@end

In the implementation we simply map the JSON id value to our recId field, since the ‘id’ is a reserved keyword in Objc:

@implementation AGTask

@synthesize recId;
@synthesize title;
@synthesize description;

+ (NSDictionary *)externalRepresentationKeyPathsByPropertyKey {
    return [super.externalRepresentationKeyPathsByPropertyKey
       mtl_dictionaryByAddingEntriesFromDictionary:@{
           @"recId": @"id"  // map the 'id' JSON string to our recId field!
     }];
}
@end

Using AeroGear

The AeroGear API is explained in here, but below is a quick snippet, how to create a AGPipe instance to send the object to the server:

// NSURL object:
NSURL* serverURL = [NSURL URLWithString:@"http://myserver.com/application/"];

// create the 'todo' pipeline, which points to the baseURL of the REST application
AGPipeline* todo = [AGPipeline pipeline:serverURL];

// Add a REST pipe for the 'projects' endpoint
id<AGPipe> tasks = [pipeline pipe:^(id<AGPipeConfig> config) {
    [config name:@"tasks"];
    [config type:@"REST"];
}];

Now that we have the tasks pipe, we can create a AGTask object and send it to the server, using Mantle’s externalRepresentation function:

// create a model object:
__block AGTask* task = [[AGTask alloc] init];
tasks.title = @"Create a screencast";
tasks.description = @"Show how the things work...";

// store the ObjC object on the server and replace it with the returned object
[tasks save:[task externalRepresentation] success:^(id responseObject) {
    // Replace the object with the response:
    task = [AGTask modelWithExternalRepresentation:responseObject];
} failure:^(NSError *error) {
    // when an error occurs... at least log it to the console..
    NSLog(@"SAVE: An error occured! \n%@", error);
}];

When the server has created the object, the returned (JSON) object contains a value for the “id” field, specified by the server. Inside of the success block, we update the task object with its latest representation, using another utility from Mantle, the modelWithExternalRepresentation class method.

Outlook

Right now the AeroGear library does not (yet?) internally use the Mantle framework, but the TODO demo app is using Mantle on a branch – for evaluation an inspiration!

Enjoy!

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: