<?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 Anyanwu-Ebere Chiemekam on Medium]]></title>
        <description><![CDATA[Stories by Anyanwu-Ebere Chiemekam on Medium]]></description>
        <link>https://medium.com/@aceanyanwu02?source=rss-1b0e19397351------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*XRSaAa9oUK9KhcWEpeD4XA.jpeg</url>
            <title>Stories by Anyanwu-Ebere Chiemekam on Medium</title>
            <link>https://medium.com/@aceanyanwu02?source=rss-1b0e19397351------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sat, 28 Nov 2026 17:43:36 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@aceanyanwu02/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[Using LiveData with Room and Kotlin]]></title>
            <link>https://medium.com/@aceanyanwu02/using-livedata-with-room-and-kotlin-3ea88cdf6fe2?source=rss-1b0e19397351------2</link>
            <guid isPermaLink="false">https://medium.com/p/3ea88cdf6fe2</guid>
            <category><![CDATA[kotlin]]></category>
            <category><![CDATA[room-with-livedata]]></category>
            <category><![CDATA[kotlinandroid]]></category>
            <category><![CDATA[android]]></category>
            <category><![CDATA[room-database]]></category>
            <dc:creator><![CDATA[Anyanwu-Ebere Chiemekam]]></dc:creator>
            <pubDate>Fri, 26 Jun 2020 23:37:43 GMT</pubDate>
            <atom:updated>2020-06-26T23:37:43.552Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="Android OS logo" src="https://cdn-images-1.medium.com/max/1024/1*rDROHHQiiMS8iQUo-kVOkg.jpeg" /></figure><p>In this article I am going to talk about how to use LiveData with Room and Kotlin.</p><p>Room is a library which acts as a layer of abstraction over SQLite and assists android developers in building databases with the in built SQLite on the Android platform.</p><p>LiveData is an observable data holder class which is life cycle aware. This means that the LiveData only updates its observers where there is an active life cycle.</p><blockquote>LiveData considers an observer, which is represented by the <a href="https://developer.android.com/reference/androidx/lifecycle/Observer">Observer</a> class, to be in an active state if its lifecycle is in the <a href="https://developer.android.com/reference/androidx/lifecycle/Lifecycle.State#STARTED">STARTED</a> or <a href="https://developer.android.com/reference/androidx/lifecycle/Lifecycle.State#RESUMED">RESUMED</a> state. LiveData only notifies active observers about updates. Inactive observers registered to watch <a href="https://developer.android.com/reference/androidx/lifecycle/LiveData">LiveData</a> objects aren&#39;t notified about changes.</blockquote><blockquote>You can register an observer paired with an object that implements the <a href="https://developer.android.com/reference/androidx/lifecycle/LifecycleOwner">LifecycleOwner</a> interface. This relationship allows the observer to be removed when the state of the corresponding <a href="https://developer.android.com/reference/androidx/lifecycle/Lifecycle">Lifecycle</a> object changes to <a href="https://developer.android.com/reference/androidx/lifecycle/Lifecycle.State#DESTROYED">DESTROYED</a>. This is especially useful for activities and fragments because they can safely observe <a href="https://developer.android.com/reference/androidx/lifecycle/LiveData">LiveData</a> objects and not worry about leaks—activities and fragments are instantly unsubscribed when their lifecycles are destroyed.</blockquote><p>Here is a video about LiveData in the official YouTube Android Developers channel.</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fwww.youtube.com%2Fembed%2FOMcDk2_4LSk%3Ffeature%3Doembed&amp;display_name=YouTube&amp;url=https%3A%2F%2Fwww.youtube.com%2Fwatch%3Fv%3DOMcDk2_4LSk&amp;image=https%3A%2F%2Fi.ytimg.com%2Fvi%2FOMcDk2_4LSk%2Fhqdefault.jpg&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=youtube" width="854" height="480" frameborder="0" scrolling="no"><a href="https://medium.com/media/8dc1604a1a15b9c63ae1f392c92114ec/href">https://medium.com/media/8dc1604a1a15b9c63ae1f392c92114ec/href</a></iframe><h3>Why use LiveData?</h3><ol><li>You prevent memory leaks in your application.</li><li>You reduce the amount of code you write.</li><li>Your data is always up to date.</li><li>Your UI is always in sync with your current data value.</li><li>No crashes due to stopped activities.</li></ol><h3>Getting started with Live Data and Room.</h3><p><strong>Step1:</strong> Add the dependencies for Room and LiveData to your app level build.gradle file.</p><pre><em>//for livedata<br></em>implementation &quot;androidx.lifecycle:lifecycle-extensions:2.2.0&quot;</pre><pre><em>//for room</em><br>implementation &quot;androidx.room:room-runtime:2.2.5&quot;<br>kapt &quot;androidx.room:room-compiler:2.2.5&quot;</pre><p><strong>Step2:</strong> Create your DAO using LiveData</p><p>We use LiveData when we want to observe and output from the database. Here it is going to be a list of <strong>NoteEntity.</strong></p><pre>@Dao<br>interface NoteDao{<br>    @Insert<br>    fun insertNote(note: NoteEntity)</pre><pre>    @Query(&quot;SELECT * FROM notedatabase&quot;)<br>    fun getAllNotes(): LiveData&lt;List&lt;NoteEntity&gt;&gt;<br>}</pre><p>We wrapped the return value of getAllNotes() with LiveData which means we’ll observe it.</p><p><strong>Step3:</strong> Observe the return value in your ViewModel (if you use MVVM) or Fragment/Activity</p><pre>var noteSize: Int? = null<br>NoteDatabase.noteDao.getAllNotes.observe{lifeCycleOwner, Observer{it<br>        noteSize = it.size<br>    }<br>}</pre><p>In the above code <em>NoteDatabase.noteDao.getAllNotes returns the LiveData of notes. </em>Using the <em>.observe </em>method makes you able to use the actual value of the returned LiveData. <em>.observe</em> takes 2 parameters:</p><ol><li>A lifecycle owner (activity or fragment)</li><li>A lamda <em>Observer{}</em></li></ol><p>In the <em>Observer{} </em>you have the value of the LiveData as <em>it </em>(you can change it to whatever you want) with this <em>it</em> you can do whatever you want with the LiveData Value.</p><p>Now your UI will be updated automatically anytime a new note is inserted.</p><h3>NOTE: You must observe the LiveData only on the UI thread else your app will crash.</h3><p>Thank you for reading my article.</p><p>Here is my <a href="https://twitter.com/emekarr">Twitter</a></p><p>And <a href="http://instagram.com/emekarr">Instagram</a></p><p>And <a href="https://www.linkedin.com/in/chiemekam-anyanwu-ebere-a2553918a?challengeId=AQHBFs02MMGkQQAAAXLy87uIDuz3zyuFauGN9OWWllGeK6rIkUfZ3U_9e_Frr3IBKPpCuwgMlN5bkByCtP8CceGS5pOiSRjs9Q&amp;submissionId=fc3f5e83-a73b-1c16-48be-24b811fa693b">Linkedin</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=3ea88cdf6fe2" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>