<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>
<channel>
	<title>Two Toasters</title>
	<atom:link href="http://twotoasters.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://twotoasters.com</link>
	<description>Just another WordPress weblog</description>
	<lastBuildDate>Tue, 08 May 2012 15:07:41 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
<xhtml:meta xmlns:xhtml="http://www.w3.org/1999/xhtml" name="robots" content="noindex" />
		<item>
		<title>Introducing RestKit</title>
		<link>http://twotoasters.com/2010/04/06/introducing-restkit/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=introducing-restkit</link>
		<comments>http://twotoasters.com/2010/04/06/introducing-restkit/#comments</comments>
		<pubDate>Tue, 06 Apr 2010 15:27:58 +0000</pubDate>
		<dc:creator>Blake Watters</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[two toasters]]></category>
		<guid isPermaLink="false">http://twotoasters.com/index.php/2010/04/06/introducing-restkit-2/</guid>
		<description><![CDATA[After reading Luke Redpath&#8217;s recent article about Core Resource and his own RestfulCoreData library, I knew we had been waiting far too long to introduce our restful modeling library to the world. So on that note I would like to introduce the Two Toasters&#8217; solution for syncing Cocoa objects with the cloud: RestKit. Design &#38; [...]]]></description>
			<content:encoded><![CDATA[<p>After reading <a href="http://lukeredpath.co.uk/blog/synching-web-services-with-core-data.html">Luke Redpath&#8217;s recent article</a> about <a href="http://coreresource.org/">Core Resource</a> and his own <a href="http://github.com/lukeredpath/RestfulCoreData">RestfulCoreData library</a>, I knew we had been waiting far too long to introduce our restful modeling library to the world. So on that note I would like to introduce the Two Toasters&#8217; solution for syncing Cocoa objects with the cloud: RestKit.</p>
<h3>Design &amp; Feature Set</h3>
<p>RestKit is a mature library providing a layer between your app&#8217;s domain objects and a restful backend in the cloud. It enables you to live in a world of Cocoa objects and then serialize them to and from your backend system with minimum fuss. RestKit aims to stay out of your way and make interacting with the web as simple and painless as possible. RestKit can currently be considered production quality and is in use on <a href="http://www.gateguruapp.com/">GateGuru</a> as well as a number of other Two Toasters apps thriving in the App Store.</p>
<p>It is quickly approaching 1.0, but still has some rough edges and currently lacks sample code to help others get started (rest assured we&#8217;ll have sample code ready by next week!). Let&#8217;s take a quick tour of the feature-set:</p>
<ul>
<li> Simple, clean API designed to be succinct and Cocoa like</li>
<li>Works on OS X and iPhone OS</li>
<li>Minimal external dependencies, currently the only external dependency is on json-framework</li>
<li> Provides robust wrappers for the request/response cycle with MIME type and status code introspection methods (i.e. isOK, isHTML, isJSON, etc.)</li>
<li> Provides support for synchronous as well as asynchronous requests out of the box</li>
<li>Supports file uploads and other requests that cannot be URL encoded</li>
<li> Provides a key-value coding based system for model mapping from JSON payloads to Cocoa object properties</li>
<li> Allows for model mapping to Core Data backed persistent models or transient vanilla NSObjects</li>
<li> Can hydrate Core Data relationships as well as properties</li>
<li>Model mapping operations are threaded to prevent the main UI thread from becoming bogged down while network operations are handled</li>
<li> Stays focused on providing excellent transport layer support without bringing UI baggage along for the ride</li>
<li> Allows for easy integration with Three20 by using the RKRequestTTModel and RKRequestFilterableTTModel (the filterable flavor enables support for searching, filtering the loaded models via one or more NSPredicate&#8217;s, and sorting the models via an NSSortDescriptor)</li>
<li> Quick switching between development/staging/production hosts by using a baseURL and resourcePath&#8217;s instead of full URL strings</li>
<li> Just enough support for Rails conventions to make integration easy, while avoiding becoming a Rails-centric library</li>
<li> Support for a global &#8216;offline&#8217; mode that enables the use of Core Data as an object cache queried via resourcePath&#8217;s. Will not generate requests in offline mode.</li>
</ul>
<h3>Let&#8217;s check out some code&#8230;</h3>
<p>Below are some simple snippets to give you a taste of what it feels like to work with RestKit.</p>
<h4>Request/Response cycle</h4>
<p>Let&#8217;s initialize a RestKit client, fire a GET request, and then ask the RKResponse object to tell us a bit about the results of the request. Note that there are corresponding methods for POST/PUT/DELETE and associated flavors that allow passing in parameters for the request.</p>
<pre>#import &lt;RestKit/RestKit.h&gt;
RKClient* client = [RKClient clientWithBaseURL:@"http://myrestkitapp.heroku.com"];
[client isNetworkAvailable];
[client get:@"/airports" delegate:self callback:@selector(airportsDidLoadResponse:)];
- (void)airportsDidLoadResponse:(RKResponse*)response {
 NSString* responseBody = [response bodyAsString];
 if ([response isHTML]) {
 // show HTML page
 } else if ([response isJSON]) {
 // parse JSON
 } else if ([response isRedirect]) {
 // follow the redirect
 }
}
</pre>
<h4>Initialize RestKit model manager</h4>
<p>Let&#8217;s initialize the a Core Data backing store for RestKit, set the mapping format to JSON (currently the only format supported, though MessagePack is planned), and register some mappings from model classes to payload elements.</p>
<pre>RKModelManager* manager = [RKModelManager managerWithBaseURL:@"http://myrestkitapp.heroku.com"];
manager.format = RKMappingFormatJSON;
manager.objectStore = [[RKManagedObjectStore alloc] initWithStoreFilename:@"RestKitApp.sqlite"];
// Establish model mapping relationships from JSON payload elements to Cocoa classes
[manager registerModel:[GGAirport class] forElementNamed:@"airport"];
[manager registerModel:[GGZone class] forElementNamed:@"zone"];</pre>
<h4>Model Mapping</h4>
<p>Here we can see what a mappable object implementation looks like. The elementToPropertyMappings method defines a dictionary specifying how to map elements in the payload to properties of the model. The elements are extracted via <strong>valueForKeyPath</strong>, so you can traverse through the response using key-value coding instead of relying on direct 1 to 1 mappings from your payload to your models.</p>
<pre>@implementation GGAirport
@dynamic city;
@dynamic code;
@dynamic name;
@dynamic state;
@dynamic country;
@dynamic airportId;
+ (NSString*)primaryKey {
 return @"airportId";
}
+ (NSDictionary*)elementToPropertyMappings {
 return [NSDictionary dictionaryWithObjectsAndKeys:
 @"city", @"city",
 @"code", @"code",
 @"name", @"name",
 @"state", @"state",
 @"country", @"country",
 @"airportId", @"id",
 nil];
}
// Sub-models included in the payload and specified here will be model-mapped
// and then assigned as the target for any Core Data associations
+ (NSDictionary*)elementToRelationshipMappings {
 return [NSDictionary dictionaryWithKeysAndObjects:
 @"zone", @"zone",
 nil];
}
@end
</pre>
<p>Here we see an example of how to work with the manager to load mappable models from the remote system. Unlike the client implementation which is callback based for flexibility, the model loader uses a simple protocol that abstracts successful and failed model loads into two methods.</p>
<pre>@implementation GGAirportConsumerExample
- (void)loadAirports {
 [[RKModelManager manager] loadModels:@"/airports" delegate:self];
}
- (void)modelLoaderRequest:(RKRequest*)request didLoadModels:(NSArray*)models response:(RKResponse*)response model:(id&lt;RKModelMappable&gt;)model {
 for (GGAirport* airport in models) {
 NSLog(@"I loaded an airport named %@", airport.name);
 }
}
- (void)modelLoaderRequest:(RKRequest*)request didFailWithError:(NSError*)error response:(RKResponse*)response model:(id&lt;RKModelMappable&gt;)model {
 NSLog(@"**** Model Loader Request failed with error : %@", [error localizedDescription]);
}
@end</pre>
<h3>Where do we go from here?</h3>
<p>I hope I&#8217;ve given you enough of a taste of RestKit to get excited and dive in. We&#8217;ve set up a Google Group as well as the normal channels of communication:</p>
<ul>
<li>Write back in the comments</li>
<li>Fork us on GitHub: <a href="http://github.com/twotoasters/RestKit">http://github.com/twotoasters/RestKit</a></li>
<li>Join the mailing list: <a href="http://groups.google.com/group/restkit">http://groups.google.com/group/restkit</a></li>
<li>Hit us up on Twitter: <a href="http://www.twitter.com/twotoasters">http://www.twitter.com/twotoasters</a></li>
</ul>
<p>Additional documentation, API polish, and sample code are in the works and on the way. Please let us know what you think and how you are using RestKit.</p>
]]></content:encoded>
			<wfw:commentRss>http://twotoasters.com/2010/04/06/introducing-restkit/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>

