<?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 Henry Tao on Medium]]></title>
        <description><![CDATA[Stories by Henry Tao on Medium]]></description>
        <link>https://medium.com/@henrytao?source=rss-e5a925414b02------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*6Jw8L35hZ0lHokTF_nga4Q.jpeg</url>
            <title>Stories by Henry Tao on Medium</title>
            <link>https://medium.com/@henrytao?source=rss-e5a925414b02------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Fri, 27 Nov 2026 19:07:35 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@henrytao/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[Chaining LiveData like RxJava with Kotlin extension]]></title>
            <link>https://medium.com/google-developer-experts/chaining-livedata-like-rxjava-with-kotlin-extension-e3a2c15ac11?source=rss-e5a925414b02------2</link>
            <guid isPermaLink="false">https://medium.com/p/e3a2c15ac11</guid>
            <category><![CDATA[livedata]]></category>
            <category><![CDATA[kotlin]]></category>
            <category><![CDATA[architecture]]></category>
            <category><![CDATA[android]]></category>
            <category><![CDATA[rxjava]]></category>
            <dc:creator><![CDATA[Henry Tao]]></dc:creator>
            <pubDate>Fri, 18 May 2018 00:42:39 GMT</pubDate>
            <atom:updated>2018-05-22T08:41:44.870Z</atom:updated>
            <content:encoded><![CDATA[<p>If you haven’t read my article about <a href="https://medium.com/@henrytao/nonnull-livedata-with-kotlin-extension-26963ffd0333">NonNull LiveData</a>, you need to read it. Same idea will be reused here.</p><p>If you don’t know about RxJava, you also need to read about it from <a href="https://github.com/ReactiveX/RxJava">https://github.com/ReactiveX/RxJava</a>.</p><p>In this article, we will talk about how we can borrow some ideas from RxJava and apply them to LiveData. Of course, thanks to Kotlin extension. It helps developer life easier. Sorry Java! The goal will look like this:</p><pre>val liveData: MutableLiveData&lt;Boolean&gt; = MutableLiveData()<br>liveData<br>  .distinct()<br>  .filter { it == false }<br>  .map { true }<br>  .nonNull()<br>  .observe(lifecycleOwner, { result -&gt;<br>    // result is non-null and always true<br>  })</pre><h3>The simplest approach</h3><p>Let’s take distinct as an example.</p><pre>val liveData: MutableLiveData&lt;Boolean&gt; = MutableLiveData()<br>liveData<br>  .distinct()<br>  .observe(lifecycleOwner, { result -&gt;<br>    // new result is different from previous result<br>  })</pre><p>In order to do as above, you need to create distinct and observe extension for LiveData.</p><pre><strong>fun </strong>&lt;T&gt; LiveData&lt;T&gt;.distinct(): LiveData&lt;T&gt; {<br>    <strong>val </strong>mediatorLiveData: MediatorLiveData&lt;T&gt; = MediatorLiveData()<br>    mediatorLiveData.addSource(<strong>this</strong>) <strong>{<br>        if </strong>(<strong>it </strong>!= mediatorLiveData.<em>value</em>) {<br>            mediatorLiveData.<em>value </em>= <strong>it<br>        </strong>}<br>    <strong>}<br>    return </strong>mediatorLiveData<br>}</pre><pre><strong>fun </strong>&lt;T&gt; LiveData&lt;T&gt;.observe(owner: LifecycleOwner, observer: (t: T?) -&gt; Unit) {<br>    observe(owner, <em>Observer </em><strong>{ </strong>observer(<strong>it</strong>) <strong>}</strong>)<br>}</pre><p>You can follow the same pattern for other methods like filter, map …</p><h3>Supporting NonNull</h3><p>Supporting NonNull is a bit tricky. As I mentioned in <a href="https://medium.com/@henrytao/nonnull-livedata-with-kotlin-extension-26963ffd0333">NonNull LiveData</a> article, you need to have NonNullMediatorLiveData to differentiate between null and non-null observer. Therefore, you may need to duplicate you logic for each of extension method. Your filter extension will look like this:</p><pre><strong>fun </strong>&lt;T&gt; LiveData&lt;T&gt;.distinct(): LiveData&lt;T&gt; {<br>    <strong>val </strong>mediatorLiveData: MediatorLiveData&lt;T&gt; = MediatorLiveData()<br>    mediatorLiveData.addSource(<strong>this</strong>) <strong>{<br>        if </strong>(<strong>it </strong>!= mediatorLiveData.<em>value</em>) {<br>            mediatorLiveData.<em>value </em>= <strong>it<br>        </strong>}<br>    <strong>}<br>    return </strong>mediatorLiveData<br>}</pre><pre>// Exactly same logic but return NonNullMediatorLiveData<br><strong>fun </strong>&lt;T&gt; NonNullMediatorLiveData&lt;T&gt;.distinct(): LiveData&lt;T&gt; {<br>    <strong>val </strong>mediatorLiveData: NonNullMediatorLiveData&lt;T&gt; = NonNullMediatorLiveData()<br>    mediatorLiveData.addSource(<strong>this</strong>) <strong>{<br>        if </strong>(<strong>it </strong>!= mediatorLiveData.<em>value</em>) {<br>            mediatorLiveData.<em>value </em>= <strong>it<br>        </strong>}<br>    <strong>}<br>    return </strong>mediatorLiveData<br>}</pre><pre><strong>fun </strong>&lt;T&gt; LiveData&lt;T&gt;.observe(owner: LifecycleOwner, observer: (t: T?) -&gt; Unit) {<br>    observe(owner, <em>Observer </em><strong>{ </strong>observer(<strong>it</strong>) <strong>}</strong>)<br>}</pre><p>Now you can do distinct and nonNull together despite the order.</p><pre>val liveData: MutableLiveData&lt;Boolean&gt; = MutableLiveData()<br>liveData<br>  .nonNull()<br>  .distinct()<br>  .observe(lifecycleOwner, { result -&gt;<br>    // new result is different from previous result<br>  })</pre><pre>// or <br>liveData<br>  .distinct()<br>  .nonNull()<br>  .observe(lifecycleOwner, { result -&gt;<br>    // new result is different from previous result<br>  })</pre><h3>Polishing code with MediatorObserver</h3><p>Duplicating logic is not nice. We can improve it by defining interface MediatorObserver&lt;IN, OUT&gt;. You code will look cleaner like this:</p><pre><strong>private class </strong>DistinctExt&lt;T&gt; : MediatorObserver&lt;T, T&gt; {<br><br>    <strong>override fun </strong>run(source: LiveData&lt;T&gt;, mediator: MediatorLiveData&lt;T&gt;, value: T?) {<br>        <strong>if </strong>(value != mediator.<em>value</em>) {<br>            mediator.<em>value </em>= value<br>        }<br>    }<br>}<br><br><strong>fun </strong>&lt;T&gt; LiveData&lt;T&gt;.distinct(): LiveData&lt;T&gt; = <em>createMediator</em>(<strong>this</strong>, DistinctExt())<br><strong>fun </strong>&lt;T&gt; NonNullMediatorLiveData&lt;T&gt;.distinct(): NonNullMediatorLiveData&lt;T&gt; = <em>createMediator</em>(<strong>this</strong>, DistinctExt())</pre><p>For more information, take a look at <a href="https://github.com/henrytao-me/livedata-ktx/blob/master/livedata-ktx/src/main/java/me/henrytao/livedataktx/LiveData.kt">LiveData.kt</a>.</p><h3>Ready to use library</h3><p>You can try out <a href="https://github.com/henrytao-me/livedata-ktx">https://github.com/henrytao-me/livedata-ktx</a>. Adding one line of code in your build.gradle and ready to rock:</p><pre>implementation &quot;me.henrytao:livedata-ktx:LATEST_VERSION&quot;</pre><h3>Feel missing methods?</h3><p>Please suggest what you need by creating <a href="https://github.com/henrytao-me/livedata-ktx/issues">issues</a>. I will support it as fast as I can.</p><p>Let me know your thought and happy coding!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=e3a2c15ac11" width="1" height="1" alt=""><hr><p><a href="https://medium.com/google-developer-experts/chaining-livedata-like-rxjava-with-kotlin-extension-e3a2c15ac11">Chaining LiveData like RxJava with Kotlin extension</a> was originally published in <a href="https://medium.com/google-developer-experts">Google Developer Experts</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[NonNull LiveData with Kotlin extension]]></title>
            <link>https://proandroiddev.com/nonnull-livedata-with-kotlin-extension-26963ffd0333?source=rss-e5a925414b02------2</link>
            <guid isPermaLink="false">https://medium.com/p/26963ffd0333</guid>
            <category><![CDATA[kotlin]]></category>
            <category><![CDATA[android-app-development]]></category>
            <category><![CDATA[mobile-app-development]]></category>
            <category><![CDATA[android]]></category>
            <category><![CDATA[kotlin-extension]]></category>
            <dc:creator><![CDATA[Henry Tao]]></dc:creator>
            <pubDate>Fri, 20 Apr 2018 12:06:37 GMT</pubDate>
            <atom:updated>2018-04-24T12:45:06.940Z</atom:updated>
            <content:encoded><![CDATA[<p>If you are currently using Google Android Architecture Components, you probably know what LiveData is. If not, you can check it out at <a href="https://developer.android.com/topic/libraries/architecture/index.html">https://developer.android.com/topic/libraries/architecture/index.html</a>.</p><p>In this post, we are going to discuss a few ways to make LiveData a lot easier to use, especially making it NonNull safe. We also use Kotlin instead of Java (Sorry Java).</p><h3>The common way to use NonNull LiveData</h3><p>Let’s see this example:</p><pre><strong>val </strong>fooLiveData: MutableLiveData&lt;Boolean&gt; = MutableLiveData()<br>fooLiveData.observe(<strong>this</strong>, <em>Observer </em><strong>{ <br>    it</strong>?.<em>let </em><strong>{ <br>        </strong><em>// Now foo is non-null<br>    </em><strong>}<br>}</strong>)</pre><p>Checking it? is not so bad, but it would be nicer if we can some how filter null value. One solution can be making NonNullObserver&lt;T&gt; by extending Observer&lt;T&gt;.</p><pre><strong>abstract class </strong>NonNullObserver&lt;T&gt; : Observer&lt;T&gt; {<br>    <br>    <strong>abstract fun </strong>onNonNullChanged(t: T)<br>    <br>    <strong>override fun </strong>onChanged(t: T?) {<br>        t?.<em>let </em><strong>{ </strong>onNonNullChanged(<strong>it</strong>) <strong>}<br>    </strong>}<br>}</pre><pre>// Usage<br>fooLiveData.observe(<strong>this</strong>, <strong>object </strong>: NonNullObserver&lt;Boolean&gt;() {<br>    <br>    <strong>override fun </strong>onNonNullChanged(t: Boolean) {<br>        <em>// Now foo is non-null<br>    </em>}<br>})</pre><p>Although it solves the problem, the usage is really ugly.</p><h3>Solution 1: create nonNullObserve Kotlin extension</h3><pre><strong>fun </strong>&lt;T&gt; LiveData&lt;T&gt;.nonNullObserve(owner: LifecycleOwner, observer: (t: T) -&gt; Unit) {<br>    <strong>this</strong>.observe(owner, android.arch.lifecycle.<em>Observer </em><strong>{ <br>        it</strong>?.<em>let</em>(observer)<br>    <strong>}</strong>)<br>}</pre><pre>// Usage<br>fooLiveData.<em>nonNullObserve</em>(<strong>this</strong>, <strong>{<br>    </strong><em>// Now foo is non-null<br></em><strong>}</strong>)</pre><p>The usage is nicer, but you need to define new method to differentiate between normal observe and non-null observe. There is a better solution for it.</p><h3>Solution 2 (best): create nonNull Kotlin extension and NonNullMediatorLiveData&lt;T&gt;</h3><p>Firstly, we define NonNullMediatorLiveData&lt;T&gt; extending from MediatorLiveData&lt;T&gt;. The purpose of it is that we can create extension function for NonNullMediatorLiveData returned from .nonNull . If you are not familiar with MediatorLiveData, you can check it out at <a href="https://developer.android.com/reference/android/arch/lifecycle/MediatorLiveData.html">https://developer.android.com/reference/android/arch/lifecycle/MediatorLiveData.html</a></p><pre><strong>class </strong>NonNullMediatorLiveData&lt;T&gt; : MediatorLiveData&lt;T&gt;()</pre><p>Secondly, we create Kotlin extension</p><pre><strong>fun </strong>&lt;T&gt; LiveData&lt;T&gt;.nonNull(): NonNullMediatorLiveData&lt;T&gt; {<br>    <strong>val </strong>mediator: NonNullMediatorLiveData&lt;T&gt; = NonNullMediatorLiveData()<br>    mediator.addSource(<strong>this</strong>, <em>Observer </em><strong>{ it</strong>?.<em>let </em><strong>{ </strong>mediator.<em>value </em>= <strong>it } }</strong>)<br>    <strong>return </strong>mediator<br>}</pre><pre><strong>fun </strong>&lt;T&gt; NonNullMediatorLiveData&lt;T&gt;.observe(owner: LifecycleOwner, observer: (t: T) -&gt; Unit) {<br>    <strong>this</strong>.observe(owner, android.arch.lifecycle.<em>Observer </em><strong>{<br>        it</strong>?.<em>let</em>(observer)<br>    <strong>}</strong>)<br>}</pre><pre>// Usage<br>fooLiveData<br>        .<em>observe</em>(<strong>this</strong>, <em>Observer </em><strong>{<br>            </strong><em>// foo is still nullable<br>        </em><strong>}</strong>)</pre><pre>fooLiveData<br>        .<em>nonNull</em>()<br>        .<em>observe</em>(<strong>this</strong>, <strong>{<br>            </strong><em>// Now foo is non-null<br>        </em><strong>}</strong>)</pre><p>Bravo! As you can see, there is slight change in how you observe and we have very nice and safe way to observe non-null LiveData. This solution does not restrict you to set null value in the source LiveData. Therefore, you still can get nullable value when you call fooLiveData.value but it makes sense.</p><p>Let me know your thought and happy coding!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=26963ffd0333" width="1" height="1" alt=""><hr><p><a href="https://proandroiddev.com/nonnull-livedata-with-kotlin-extension-26963ffd0333">NonNull LiveData with Kotlin extension</a> was originally published in <a href="https://proandroiddev.com/">ProAndroidDev</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>