<?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 Sebastian Sellmair on Medium]]></title>
        <description><![CDATA[Stories by Sebastian Sellmair on Medium]]></description>
        <link>https://medium.com/@sellmair?source=rss-5f96be1c477d------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*MfktFRVvT_hUZyWP.</url>
            <title>Stories by Sebastian Sellmair on Medium</title>
            <link>https://medium.com/@sellmair?source=rss-5f96be1c477d------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Wed, 02 Dec 2026 19:42:12 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@sellmair/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[Disposing on Android. The right way]]></title>
            <link>https://proandroiddev.com/disposing-on-android-the-right-way-97bd55cbf970?source=rss-5f96be1c477d------2</link>
            <guid isPermaLink="false">https://medium.com/p/97bd55cbf970</guid>
            <category><![CDATA[xr]]></category>
            <category><![CDATA[lifecycle]]></category>
            <category><![CDATA[android]]></category>
            <category><![CDATA[rxjava2]]></category>
            <category><![CDATA[rxjava]]></category>
            <dc:creator><![CDATA[Sebastian Sellmair]]></dc:creator>
            <pubDate>Sun, 30 Sep 2018 18:36:32 GMT</pubDate>
            <atom:updated>2018-10-28T12:13:08.883Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*NUCsYnxXNVEh3UTyiOkBfQ.jpeg" /></figure><h4>Introduction</h4><p>The problem is more than well known: Every Android project that is using rxJava will at some time want to subscribe to data in one of the frameworks main classes like Activity or Fragment. Obviously, those classes are created and destroyed by the Android framework itself and therefore come with a given <em>Lifecycle</em>. Subscriptions to streams need to be disposed at the right time in order to avoid memory leaks. This can be done very simply: Hold a reference to the <em>Disposable</em> when created and call the <em>dispose</em> function at the right moment in one of the Lifecycle hooks like <em>onStop</em> or <em>onDestroy</em>. Even though this problem is that simple, popular libraries like <a href="https://github.com/trello/RxLifecycle">trello/RxLifecycle</a> required us to load at least two artifacts and do work in some kind of <em>BaseActivity</em> and <em>BaseFragment</em>. While I admire the level of abstraction, separation and the engineering that trello’s solution offered, I am curious why all this is necessary for such a simple problem?<br>Why do people really need such a Library? <br>Also, some of the trello/RxLifecycle’s approaches have other problems that Dan Lew, the maintainer of trello/RxLifecycle, explained well in his <a href="https://blog.danlew.net/2017/08/02/why-not-rxlifecycle/">blog post</a> to which I agree on.</p><h4>So why?</h4><p>The question is: Why have people, including myself, been using libraries like trello/RxLifecycle?</p><p><strong><em>Disposing ‘by hand’ feels ugly.</em></strong></p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/66116e7d56fa3e055737f280808690b5/href">https://medium.com/media/66116e7d56fa3e055737f280808690b5/href</a></iframe><p>This code, while perfectly functional, just <strong><em>feels</em></strong> ugly, even though it actually is not that bad. It is verbose, it is readable and clear and it is working. So why do I feel bad about it?</p><p>*<strong>Firstly</strong>*, this just breaks the aesthetics, right? You can declare almost any behavior, that you might want, inside this nice chain of operators that looks nice and compact. So all this extra code and work, even though it is not that much, just feels clunky because it is not fitting this style.</p><p>*<strong>Secondly</strong>*, this could become messy when having a more complex <em>Activity</em>, because it would require you to override each and every lifecycle callback to dispose the correct disposables.</p><p>*<strong>Thirdly</strong>*, this splits the definition of the stream and the information of ‘where should it be disposed’, which makes it harder to see what is going on and harder to maintain. You know when you want this subscription to be disposed when declaring the stream, so WHY should this information be spread across your activity?</p><p>All of this feelings obviously also apply when using <a href="https://github.com/ReactiveX/RxJava/blob/2.x/src/main/java/io/reactivex/disposables/CompositeDisposable.java"><em>CompositeDisposable</em></a><em> </em>instead of single<em> Disposable</em> like in the example above.</p><h4>What’s wrong with <a href="https://github.com/trello/RxLifecycle">trello/RxLifecycle</a> or <a href="https://github.com/uber/AutoDispose">uber/AutoDispose</a>?</h4><p>As previously mentioned, Dan Lew already gave us very good points on ‘<a href="https://blog.danlew.net/2017/08/02/why-not-rxlifecycle/">why not to use trello/RxLifecycle’</a> and I highly recommend reading it for more detail. Here are the points, that I value the most:</p><ul><li>Automatic lifecycle detection leads to confusing and sometimes non-deterministic code.</li><li>trello/RxLifecycle can only simulate: It does not dispose your ‘final <em>Disposable</em>’, but sends a ‘complete’ event to end the subscription which could lead to confusion or even subtle bugs.</li><li>It generally requires subclassing</li><li>It is far to complex for this simple problem. As previously mentioned: I do not want to include multiple artifacts, read lots of documentation and code to understand what is going on</li></ul><h4>The right way</h4><p>Using <em>CompositeDisposable</em> or similar implementations to group <em>Disposable</em> objects and dispose them at the right time. That’s it.</p><p>So should we just ignore our feelings and concerns and accept overriding the correct lifecycle hooks is necessary to correctly dispose our subscriptions?</p><p>No! It is easy to implement some <a href="https://github.com/sellmair/disposer/blob/master/lib/src/main/java/io/sellmair/disposer/Disposer.kt"><em>Disposer</em></a> object that can make use of Android’s <em>Lifecycle</em> implementation to dispose automatically at the right point in time! Once this is done, nothing hinders you from extending <em>Lifecycle</em> to provide this <em>Disposer</em> automatically! And if you want: You could also easily implement those extensions for LifecycleOwner as well. Here is what you would end up with:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/ef52e2887f2393fee186d456184d4dd9/href">https://medium.com/media/ef52e2887f2393fee186d456184d4dd9/href</a></iframe><p>The code above demonstrates two possible API’s on how to declare that the stream should be automatically disposed when ‘onStop’ is called.</p><p>In order to avoid confusion, I also showed the verbose version without the extensions shortcuts. <br>I highly recommend avoiding too complex libraries and implement this pattern by yourself. If you like, use my implementation that I published today!</p><p><a href="https://github.com/sellmair/disposer">https://github.com/sellmair/disposer</a></p><p>Some last words</p><p>We, as programmers, often fall into the trap of thinking ‘less is more’ and confuse verbosity with the dreaded ‘Boilerplate’. Not specifying the correct point of time where <em>Disposable</em> objects are disposed is not even saving you time. It is literally the same amount of characters to type. It only saves us from thinking about what we are doing and I do not know why we thought it is a good idea!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=97bd55cbf970" width="1" height="1" alt=""><hr><p><a href="https://proandroiddev.com/disposing-on-android-the-right-way-97bd55cbf970">Disposing on Android. The right way</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>
        <item>
            <title><![CDATA[Phantom Read Rights in Kotlin]]></title>
            <link>https://medium.com/@sellmair/phantom-read-rights-in-kotlin-modelling-a-pipeline-eef3523db857?source=rss-5f96be1c477d------2</link>
            <guid isPermaLink="false">https://medium.com/p/eef3523db857</guid>
            <category><![CDATA[phantom-types]]></category>
            <category><![CDATA[phantom]]></category>
            <category><![CDATA[pipeline]]></category>
            <category><![CDATA[kotlin]]></category>
            <category><![CDATA[phantom-read-rights]]></category>
            <dc:creator><![CDATA[Sebastian Sellmair]]></dc:creator>
            <pubDate>Thu, 10 May 2018 17:18:05 GMT</pubDate>
            <atom:updated>2019-02-27T14:53:07.215Z</atom:updated>
            <content:encoded><![CDATA[<p>Let’s imagine you start working on a new project for your customer. The mission: Build a machine that is able to estimate the amount of beer drunk by a crowd by processing an image of the crowd. You start thinking about how to approach the problem and you notice that it can be easily split into certain stages.</p><ul><li>Stage 1, <strong>Shape Detection: </strong>Detect low level features like shapes. Circles, Polygons, etc</li><li>Stage 2, <strong>Feature Detection</strong>: Detect higher level features like: Faces, Arms, Shoes, etc based on previously found shapes</li><li>Stage 3, <strong>Person Detection</strong>: Detect individual persons in the image based on previously found features</li><li>Stage 4, <strong>Measure</strong>: Use some fancy AI algorithm to calculate how drunk each person is and estimate the amount of beer necessary for the whole crowd</li></ul><p>So far so good! You decided to model this thing as a pipeline with those 4 stages. So what are you putting into this pipeline?</p><h4>What about: Creating a Request and inherit from it?</h4><p>Let’s jump right in. Here is the code:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/f4a74febf168bbb29682a144be932ea0/href">https://medium.com/media/f4a74febf168bbb29682a144be932ea0/href</a></iframe><p>Sooooo 🧐 Waait a second…<br>What do you mean with <em>preparedImage</em>???</p><p>Here is the thing: You are using a special framework, that is not able to work with the images of your camera directly. This means you need to provide the correct type of image by converting it. This example will use ‘Mat’ to represent this image types.</p><p>So after modelling your Request type like this you start to notice certain things: <br><strong>1</strong>. Every Request shares some attributes (image, timestamp, measureSettings)<br><strong>2</strong>. All higher stage Request are sharing preparedImage and preparedScaledImage<br><strong>3</strong>. The source image itself is not really used anymore after converting it to ‘Mat’</p><p>So what is the solution? Creating a more complex inheritance model to share attributes? Sure! But how can you close the source image after converting it? Should you separate the initial Request from all the other Request types?<br>Oh dear 🙁. This seams to get messy pretty fast, even with this almost trivial example. At this point you start feeling frustrated. You could do it, but you don’t really like the idea. There has to be a better way ¯\_(ツ)_/¯</p><h4>What about: Phantom Types</h4><p>At this moment you start thinking about a more unconventional way of handling read rights of your request object. You wonder:</p><p>What if you would have a generic type parameter which represents the stage of the request. And what if this parameter magically could have control over what you can read from this object? 🤔</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*dx0p89kBGChXLXT8q2YBmw.gif" /><figcaption>Phantom Read Rights in Action</figcaption></figure><p><em>What is happening in the gif above? <br>One can see how the read rights of certain values change by changing the phantom type of your request object. An object of type Request&lt;Stage.Pending&gt; only provides the image as ‘Image’ instead of ‘Mat’ and does not provide any scaledImage or shapes. An object of type Request&lt;Stage.FeatureDetection&gt; on the other hand cannot provide an image of type ‘Image’ but can provide shapes and image + scaledImage as ‘Mat’</em></p><p><strong>But how can this be implemented and how can this be easier than just managing multiple classes?</strong></p><ul><li><strong>Step 1</strong>: Implement the Request Class.</li></ul><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/b9987f065e2d6c08599fc1c097a85eaa/href">https://medium.com/media/b9987f065e2d6c08599fc1c097a85eaa/href</a></iframe><p>Okay. Now you have an object that is able to hold a reference to everything the pipeline could need. It contains the generic type param T which does not do anything for us and everything is private. So we cannot do anything with it. What is the point?</p><ul><li><strong>Step 2</strong>: Implement ‘Read-Right-Interfaces’</li></ul><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/d05e18907e76bd6489a233c002adcda1/href">https://medium.com/media/d05e18907e76bd6489a233c002adcda1/href</a></iframe><p>It is really as simple as creating an empty interface for every attribute you want to manage.</p><ul><li><strong>Step 3</strong>: Implement companion access for your values.</li></ul><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/d8e7537316f47e7b888fbf5a3e3e6388/href">https://medium.com/media/d8e7537316f47e7b888fbf5a3e3e6388/href</a></iframe><p>Now you have a public method to access you values for certain Phantom Types. Here is one downside of this design: You need to model your transitions (how to get a Request into a certain Stage) correctly, otherwise you would get this ‘StageException’ at runtime. You also need to find a way to prevent the construction of a Request in arbitrary Stage. Both problems are relatively easy but won’t be covered by this blog post!</p><ul><li><strong>Step 4</strong>: Create convenience extensions</li></ul><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/dc2c6a2f23a1aae64b1480c8d708598a/href">https://medium.com/media/dc2c6a2f23a1aae64b1480c8d708598a/href</a></iframe><p>You obviously don’t want to use this companion functions directly. You obviously want a familiar developer experience when working in your pipeline. The difference between straight forward read rights and ‘Phantom-Read-Rights’ won’t be really noticeable with this extensions! 😋</p><ul><li><strong>Step 5</strong>: Define your stages</li></ul><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/352c467e4298de03ab9d238b110a8e66/href">https://medium.com/media/352c467e4298de03ab9d238b110a8e66/href</a></iframe><p>You obviously want to have a Phantom Type for every stage of your pipeline. Every of those stages will be able to read the ‘MeasureSettings’ and the ‘Timestamp’.</p><p>A Stage then just extends the specific read rights that it want’s to publish and… 🎉 Yippieh! Phantom Read Rights 🎊</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*kv6TUUukQ4aGupXUT4NheQ.gif" /></figure><h4>Appendix</h4><p>The ‘Phantom Read Rights’ technique is not suitable for a broad set of problems, but it can be extremely useful for certain problems. This technique is used by QuickBird Studios for a very special customer in a very special project and has been proven very useful when modelling a pipeline.</p><p>If you are further interested in the way we at QuickBird Studios approach problems, check out our blog 😋<br><a href="https://quickbirdstudios.com/blog/">https://quickbirdstudios.com/blog/</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=eef3523db857" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>