<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Marcin Kmiec on Medium]]></title>
        <description><![CDATA[Stories by Marcin Kmiec on Medium]]></description>
        <link>https://medium.com/@Bonzoq?source=rss-8fc73c103a4a------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*CAWXJ4vKSBMQupRv.jpeg</url>
            <title>Stories by Marcin Kmiec on Medium</title>
            <link>https://medium.com/@Bonzoq?source=rss-8fc73c103a4a------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Wed, 02 Dec 2026 05:19:27 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@Bonzoq/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[RestKit Tutorial: how to fetch data from an API into Core Data.]]></title>
            <link>https://medium.com/ios-os-x-development/restkit-tutorial-how-to-fetch-data-from-an-api-into-core-data-9326af750e10?source=rss-8fc73c103a4a------2</link>
            <guid isPermaLink="false">https://medium.com/p/9326af750e10</guid>
            <category><![CDATA[ios]]></category>
            <category><![CDATA[core-data]]></category>
            <category><![CDATA[restkit]]></category>
            <dc:creator><![CDATA[Marcin Kmiec]]></dc:creator>
            <pubDate>Tue, 03 Feb 2015 22:03:11 GMT</pubDate>
            <atom:updated>2015-07-26T19:56:36.935Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*FWKyohYwlRjfw-Fozz-roQ.png" /></figure><h3>RESTKit Tutorial: how to fetch data from a RESTful API into Core Data.</h3><p><em>A complete guide to configuring RESTKit framework in order to fetch and save data, including nested arrays, from restful APIs.</em></p><p>Please also check out my other post on using Realm mobile database, a no thrills alternative to Core Data, with Mantle model framework + AFNetworking. With this delightful combination of software libraries you should be able to get your network and persistence layers up and running in no time. <a href="http://www.toptal.com/ios/simplifying-restful-api-use-and-data-persistence-on-ios-with-mantle-and-realm">http://www.toptal.com/ios/simplifying-restful-api-use-and-data-persistence-on-ios-with-mantle-and-realm</a>.</p><h4>Introduction</h4><p>This tutorial is intended for intermediate iOS developers. It assumes basic understanding of Core Data. If you’re not yet familiar with this technology, I recommend that you take a look at <a href="http://www.raywenderlich.com/934/core-data-tutorial-for-ios-getting-started">this</a> excellent introduction to Core Data by <a href="http://www.raywenderlich.com/u/acburk">Adam Burkepile</a> on Ray Wenderlich’s website.</p><p>We will be using <a href="http://www.feedzilla.com/">Feedzilla</a> news RESTful API. It provides, among other things, a list of current news articles in a number of categories. Our aim will be to fetch the list of current articles for a category of our choosing and then fetch information for each of those articles separately. Not only will we fetch that data, we will also persist it and present it in a nested table view.</p><p>This is where <a href="http://restkit.org/">RESTKit</a>, one of the most widely used iOS libraries, comes into play. It allows for seamless integration of an iOS app with a remote RESTful API. After initial configuration RESTKit fetches data taking care of the network stuff and parses the results into either your custom classes or straight into Core Data. In this tutorial we will be focusing on the latter.</p><h4>Feedzilla API overview</h4><p>As mentioned in the introduction, Feedzilla news API provides a list of news articles in different categories such as World News, Music, Sports, Programming and many others. The list of categories is available at <a href="http://api.feedzilla.com/v1/categories.json">http://api.feedzilla.com/v1/categories.json</a> . Though we will not use this API endpoint in our app directly, we need it to choose a category for which articles we’re about to fetch. Let’s assume we’re going to use the ‘Programming’ category, whose id is 16. Of course, you might as well choose any other category.</p><p>We will be fetching data in JSON format, though both the API and RESTKit itself support XML as well.</p><p>Now, the list of articles in category ‘Programming’ is available at <a href="http://api.feedzilla.com/v1/categories/16/articles.json">http://api.feedzilla.com/v1/categories/16/articles.json</a> . Quite simply, if you wish to get an article list for a different category you need to provide its id instead of the number 16 in the above url.</p><p>And here’s a sample JSON result for this enpoint.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/463019efa23ae5bb22cfdef8bd076b6d/href">https://medium.com/media/463019efa23ae5bb22cfdef8bd076b6d/href</a></iframe><p>As we can see, the resulting JSON consists of three fields: <em>title</em>, <em>syndication_url</em> and <em>description</em>, along with a nested array of articles.</p><h4>Core Data model</h4><p>Let’s start by creating an XCode project using the “Master-Detail Application” template. Please make sure the “Use Core Data” checkbox remains <strong>unchecked</strong>, as we’ll be setting up Core Data ourselves.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/726/1*naSRmpbXSHkHUjIURjPq7Q.png" /></figure><p>Now that we have an XCode project it’s time to create our Core Data model by going to <em>File -&gt; New -&gt; File -&gt; iOS -&gt; Core Data</em> and choosing <em>Data Model.</em></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/727/1*OPqQ2xwfjCznidss00umFQ.png" /></figure><p>Let’s call our model quite simply <em>DataModel</em> and press Create. Our new model should now appear in the project list:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/254/1*qDimiYttXY5naGIEE2n6JA.png" /></figure><p>Before we dwelve into the technicalities of setting up a Core Data model, let’s again have a look at the structure of the JSON that we’re eventually going to store in Core Data. It contains a nested array of articles and three additional simple fields. The question now is: how do we model this in Core Data? The answer lies in the relationship between the two entities: the article list and the article itself. In a manner similar to relational databases, we might model the relationship between an article list and articles it contains as one-to-many, since an article list contains multiple articles, while a single article belongs to exactly one article list. To sum up, we are going to create a data model containing two entities: the article list and a relationship between them.</p><p>Let’s double click the <em>DataModel.xcdatamodeld</em> to start editing.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/800/1*pwXRCcfQgQ4miieSJxCk1Q.png" /><figcaption>Core Data model definiton</figcaption></figure><p>We need to add two entities by pressing the Add Entity button (1). After renaming them to Article and ArticleList (2) it’s time to start adding attributes (3). Let’s add three attributes to ArticleList: <em>article_description</em>, <em>title</em> and <em>url</em> — all of String type. Now let’s define the attributes of the entity Article as: <em>publish_date</em>, <em>source</em>, <em>source_url</em>, <em>summary</em>,<em> title</em> and <em>url</em> — again, all of them strings except for<em> publish_date</em> which will be of type Date. Now we can change the editor style by pressing the button denoted as (4) in the figure above. The result should be similar to this:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*x1wXsgVkZei73ufOgMnm9g.jpeg" /></figure><p>What we have here are two entities <em>ArticleList</em> and <em>Article</em>. If we compare them to our JSON we can clearly identify the corresponding attributes. The names of <em>Article’s</em> attributes match exactly the attributes in the response JSON, while the names of <em>ArticleList’s</em> attributes differ slightly from those in the JSON. This is on purpose to illustrate how attributes are mapped, as we’ll see later on during RESTKit configuration.</p><p>What we’re missing at this point is the aforementioned relationship between the two entities. Let’s revert to the previous editor style and add a relationship between ArticleList and Article.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/924/1*Mf-1aLlkhvsya_ozw3ZgNA.png" /></figure><p>After choosing ArticleList (1) we add a new relationship by pressing the ‘+’ button in Relationships section (2). Let’s name this new relationship <em>articles</em> (3) and set the destination of this new relationship to Article (4). Lastly, we need to change the type of it to <em>To Many</em> (5). Now let’s again change the editor style to graphical.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/393/1*jLs_vbmJ0gwBXc-GuqHB9w.png" /></figure><p>We can see that <em>ArticleList</em> entity remains in a to-many relationship with <em>Article</em>. In other words, article list contains multiple articles. In this very model the relationship in the other direction, that is from <em>Article</em> to <em>ArticleList</em> is undefined. Though we know that a single article belongs exactly to one list, this to-one relationship from <em>Article</em> to <em>ArticleList</em> is not modelled here, as it’s not necessary. All we need to know is what articles belong to a given list and we have just created a data model to store this information.</p><p>Now that our Core Data model has been defined we can generate a model file for each entity. By going to <em>File -&gt; New -&gt; File -&gt; iOS -&gt; Core Data </em>and choosing <em>NSManagedObject subclass</em> we can automatically create Core Data model files. After pressing <em>Next </em>we choose our data model, then again click <em>Next</em> and then select the entities for which files we’d like to generate:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/717/1*_MvR_pR1tnd2CIVeGB4MSQ.png" /></figure><p>Click through this and the next dialogue to save our newly created files. They should now appear in the project list:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/250/1*rkFtRTZ8CDovocircedwSA.png" /></figure><p>Let’s have a closer look at them. What is interesting to us is that entity attributes have been declared as properties in .h files. Now if we look at the <em>ArticleList.h</em> file specifically, we’ll see that in addition to properties mirroring attributes of String type, there’s one more property of type <em>NSSet </em>called <em>articles</em>. This property will hold the articles that belong to the article list. The to-many relationship has been declared here as a property of type <em>NSSet</em>.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/11cb4369f4b799cdabe83d25e405ac2f/href">https://medium.com/media/11cb4369f4b799cdabe83d25e405ac2f/href</a></iframe><h4>Installing and initializing RESTKit</h4><p>The easiest way to install RESTKit in a project is to use CocoaPods. Details can be found on <a href="https://github.com/RestKit/RestKit">RESTKit Github page</a>. Once the RESTKit pod has been installed, let’s open the project’s newly created workspace file. We should now be able to see RESTKit library installed with a few additional libraries such as AFNetworking.</p><p>Having installed RESTKit it’s time to initialize it. The best place to do this is the application delegate. And so, let’s add the following two import statements to the <em>AppDelegate.m </em>file:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/8bf6a8a742dd4efa3e773b5a2eed720a/href">https://medium.com/media/8bf6a8a742dd4efa3e773b5a2eed720a/href</a></iframe><p>We’ll place RESTKit configuration code in <em>application:didFinishLaunchingWithOptions: </em>method just above <em>return YES; </em>statement:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/247f1a373124505a869c41333bc621a0/href">https://medium.com/media/247f1a373124505a869c41333bc621a0/href</a></iframe><p>First off, we need to initialize RESTKit’s object manager with the base URL of the API we’re using:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/41162277552cbf85fa9f4e73a5189ae2/href">https://medium.com/media/41162277552cbf85fa9f4e73a5189ae2/href</a></iframe><p>Next, we need to tell RESTKit to use the Core Data model we have so meticulously created . The method <em>[NSManagedObjectModel mergedModelFromBundles:nil] </em>will return all the models from the main bundle merged into a single model. Since we have only one model in our bundle, we will get exactly it. Now we can create an object of <em>RKManagedObjectStore </em>type. We will initialize it with our <em>managedObjectModel</em>. Lastly, we need to assign this resulting object to the <em>managedObjectStore</em> property of RESTKit’s <em>objectManager</em>.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/7f1b904c3270597bacdb15042ef213b0/href">https://medium.com/media/7f1b904c3270597bacdb15042ef213b0/href</a></iframe><p>The next step is to complete Core Data stack initialization. The following piece of code initializes a persistent store coordinator, creates managed object contexts and configures managed object cache.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/7b2a5850866ba23ec8f6e199c0a77d9a/href">https://medium.com/media/7b2a5850866ba23ec8f6e199c0a77d9a/href</a></iframe><p>In result, our <em>application:didFinishLaunchingWithOptions: </em>method should resemble the following:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/ddcc990b63bbc30b37f5b3150136ce87/href">https://medium.com/media/ddcc990b63bbc30b37f5b3150136ce87/href</a></iframe><h4>RESTKit entity mapping</h4><p>Now that we have initialized RESTKit and Core Data stack it’s time to define mappings. To put it simply, we have to tell RESTKit how it should map API responses into local Core Data objects.</p><p>Let’s start by defining a mapping for the entity <em>ArticleList, </em>which we earlier defined in the Core Data model. We start by creating an object of <em>RKEntityMapping</em> class, which we will now use to define this mapping. Next, we need to specify a unique identification attribute, or a combination of attributes, in order to identify instances of our Core Data entity. The attribute <em>title</em> is a good candidate for <em>ArticleList</em>’s unique indentifier, as we can safely assume that all article lists provided by the Feedzilla API have unique titles.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/ac9d8bc79e69e71a703a2ad337a44c0a/href">https://medium.com/media/ac9d8bc79e69e71a703a2ad337a44c0a/href</a></iframe><p>Now comes the most important part. We need to define how the incoming JSON objects will be mapped into local Core Data objects. As a reminder here’s the structure of that JSON:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/5c412069ae71007e736ade63812fb8e6/href">https://medium.com/media/5c412069ae71007e736ade63812fb8e6/href</a></iframe><p>It contains three fields: <em>title</em>, <em>syndication_url </em>and <em>description </em>as well as a nested array <em>articles</em>. Let’s have a look now at the <em>ArticleList.h</em> file which is a model file for the Core Data entity <em>ArticleList.</em> It contains the following properties:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/15f253edb2138135532451374c154c66/href">https://medium.com/media/15f253edb2138135532451374c154c66/href</a></iframe><p>What we need to do now is to define mappings from the JSON object into properties of the <em>ArticleList</em> class. Let’s start with the three simple JSON fields: <em>title</em>, <em>syndication_url </em>and <em>description</em>, which we are now going to map into the following properties of <em>ArticleList</em> class: <em>title, url</em> and <em>article_description</em>. We will do it by invoking <em>addAttributeMappingsFromDictionary: </em>method on the <em>articleListMapping</em> object that we just created. This method takes an <em>NSDictionary </em>object as the only parameter.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/66df75a63d4cdf04c17200e498168aa8/href">https://medium.com/media/66df75a63d4cdf04c17200e498168aa8/href</a></iframe><p>Looking at the above, mapping attributes seems pretty straightforward. Dictionary keys refer to field names in the incoming object, while the corresponding values refer to the properties in the local model file.</p><p>What’s left now is to define a mapping for the nested <em>articles</em> array. Before we do that, we need to define mappings for the <em>Article</em> Core Data entity. If we compare the <em>Article </em>class generated from our Core Data model:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/bca3a180c299519eb158ce62b2d4f3ff/href">https://medium.com/media/bca3a180c299519eb158ce62b2d4f3ff/href</a></iframe><p>with the JSON object that we get from the API, we’ll see that field names in the JSON match exactly the names of properties in the <em>Article</em> class. In such case, instead of using an <em>NSDictionary</em>, we may use a simple NSArray:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/731a215317af887fdaf4a8554d9de837/href">https://medium.com/media/731a215317af887fdaf4a8554d9de837/href</a></iframe><p>Finally, here’s how we define a mapping for the nested <em>articles</em> array:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/6485090a57b11467dd6a2aadd7d25cc7/href">https://medium.com/media/6485090a57b11467dd6a2aadd7d25cc7/href</a></iframe><p>What we do here is we add a relationship mapping to the <em>articleListMapping</em> object we already declared. This new mapping is from the nested array <em>articles</em> in the incoming JSON object to the property <em>articles</em> in the Core Data entity <em>ArticleList. </em>Moreover, we declare that the nested array contains objects that should be mapped using <em>articleMapping</em>, that is into instances of Core Data entity <em>Article.</em></p><p>Last, but not least, we have to register our <em>articleListMapping </em>so that it is used with the API endpoint that returns in response a list of articles in the chosen category. We do so by initializing an object of <em>RKResponseDescriptor</em> class and adding it to RESTKit’s <em>objectManager</em>. When declaring a response descriptor we define a mapping and a path pattern. From now on, all API responses from endpoints defined by the specified path pattern will mapped using the specified mapping.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/f3df186b047fa9df5601f6bf459b80eb/href">https://medium.com/media/f3df186b047fa9df5601f6bf459b80eb/href</a></iframe><p>Remember how we defined the <em>objectManager</em> using a base url? That base url was <em>http://api.feedzilla.com. </em>After the response descriptor was added to the <em>objectManager</em> all responses from the API at path: <em>http://api.feedzilla.com/v1/categories/:id/articles.json</em> will be mapped using <em>articleListMapping</em>. The colon in <em>:id</em> denotes <em>id </em>as a parameter. So for instance both responses from <em>http://api.feedzilla.com/v1/categories/</em><strong><em>16</em></strong><em>/articles.json </em>and from <em>http://api.feedzilla.com/v1/categories/</em><strong><em>8</em></strong><em>/articles.json </em>will be mapped using <em>articleListMapping</em>.</p><p>We can also enable the activity indicator in the application’s top bar, which will be helpful in case the API takes some time to return data.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/6366b2b2317167d784f2f1f371836fa5/href">https://medium.com/media/6366b2b2317167d784f2f1f371836fa5/href</a></iframe><p>Below is the full code that we have written so far to configure mappings. Again, we can place it in <em>application:didFinishLaunchingWithOptions: </em>method just above <em>return YES; </em>statement.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/d6957858f08601d2d757a5a89f8a5057/href">https://medium.com/media/d6957858f08601d2d757a5a89f8a5057/href</a></iframe><h4>First API request</h4><p>Now that we have configured RESTKit it’s time to make our very first request and see the response mapped into a Core Data object. In <em>MasterViewController.m</em> file let’s add a property to hold the incoming list of articles in an NSArray:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/3289df3ff7e264d3717a806d1ee6c691/href">https://medium.com/media/3289df3ff7e264d3717a806d1ee6c691/href</a></iframe><p>Now let’s add the two following methods at the end of the file:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/9cd704da0b77af400c9d0ac81a006716/href">https://medium.com/media/9cd704da0b77af400c9d0ac81a006716/href</a></iframe><p>In method <em>requestData</em> a RESTKit request to the provided API endpoint is made. If the request is successful, the success block of code is invoked by which time the incoming data has been safely saved to Core Data. In that block of code the other method, <em>fetchArticlesFromContext</em> is invoked. What follows is a standard Core Data fetch request on the main queue managed context provided by RESTKit’s default object store. In result an array of articles is assigned to the <em>self.articles</em> property. Please note that <em>articleList.articles </em>is an <em>NSSet </em>object therefore the method <em>allObjects</em> is invoked on it to obtain an <em>NSArray </em>of objects.</p><p>The very last step is to configure the table view. It’s pretty straightforward, we will display articles’ titles in consecutive cells, while pressing on a cell will take us to the detail view which in turn shall display a summary of the chosen article. Method <em>requestData </em>is invoked in <em>viewDidLoad</em>. Moreover, unnecessary code used to handle cell editing was also removed. Here’s the complete <em>MasterViewController.</em></p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/c41f49ab8dd88fa0b97719889c5112c3/href">https://medium.com/media/c41f49ab8dd88fa0b97719889c5112c3/href</a></iframe><p>In effect we have a functioning master-detail application. Since all the data fetched from the API is stored in CoreData, it is available offline.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/320/1*FfEBsRGlS0fb5OAHoGOW0Q.gif" /></figure><h4>Conclusions</h4><p>In this tutorial we learned how to use RESTKit framework to fetch data from remote APIs and store it in Core Data. We created a Core Data model for a nested array of objects and properly configured an object mapping for it.</p><p>The main advantage of using RESTKit is that once models for local objects are created and mappings are set up, interaction with RESTful services is simple and fast. That’s because handling network connections, parsing JSONs and mapping objects is all taken care of!</p><p>The final project is available at <a href="https://github.com/bonzoq/RESTKitTutorial">https://github.com/bonzoq/RESTKitTutorial</a> .</p><p><strong>Thanks </strong>to Paweł Kowalczyk for reading drafts of this.</p><p><strong><em>Marcin Kmiec</em></strong> is an iOS developer and consultant. You can reach him through his <a href="http://mkmiec.com/">website</a>.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=9326af750e10" width="1" height="1" alt=""><hr><p><a href="https://medium.com/ios-os-x-development/restkit-tutorial-how-to-fetch-data-from-an-api-into-core-data-9326af750e10">RestKit Tutorial: how to fetch data from an API into Core Data.</a> was originally published in <a href="https://medium.com/ios-os-x-development">iOS App Development</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>