<?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 Maciej Witowski on Medium]]></title>
        <description><![CDATA[Stories by Maciej Witowski on Medium]]></description>
        <link>https://medium.com/@maciejwitowski?source=rss-2c408d374281------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*ko-fkYwErqwjflCUknCoAg.png</url>
            <title>Stories by Maciej Witowski on Medium</title>
            <link>https://medium.com/@maciejwitowski?source=rss-2c408d374281------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Fri, 20 Nov 2026 19:02:32 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@maciejwitowski/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[Testing aggregate SQLite queries in Robolectric]]></title>
            <link>https://medium.com/@maciejwitowski/testing-aggregate-sqlite-queries-in-robolectric-3a93512cf60a?source=rss-2c408d374281------2</link>
            <guid isPermaLink="false">https://medium.com/p/3a93512cf60a</guid>
            <category><![CDATA[robolectric]]></category>
            <category><![CDATA[sqlite]]></category>
            <category><![CDATA[android]]></category>
            <dc:creator><![CDATA[Maciej Witowski]]></dc:creator>
            <pubDate>Mon, 20 May 2019 16:05:38 GMT</pubDate>
            <atom:updated>2019-05-20T20:15:15.699Z</atom:updated>
            <content:encoded><![CDATA[<h3>Testing aggregate SQLite queries with Robolectric</h3><p>I’ve recently worked on a feature which requires fetching some aggregate data from SQLite. For simplicity, let’s assume I had a table called Tasks with columns name and created_at and I wanted to fetch the name of the oldest Task.</p><p>This simple query should do the job:</p><p>SELECT name, min(created_at) FROM tasks;</p><p>To unit test such queries in our project, we use Robolectric since we need a database to execute the queries. Surprisingly, it turned out my tests weren’t passing — I was getting the minimal values of created_at, but they weren’t necessarily paired with the name of the Tasks they belonged to.</p><p>The reason was that the version of Robolectric we use (the latest one, 4.2.1), supports SQLite 3.7.10 and looking at the <a href="https://www.sqlite.org/changes.html">SQLite’s changelog</a> we can see that one of the changes introduced in the subsequent version 3.7.11 (released in March 2012!) is:</p><blockquote>Queries of the form: “SELECT max(x), y FROM table” returns the value of y on the same row that contains the maximum x value.</blockquote><p>This means that prior to SQLite 3.7.11, we have no guarantee of which Task‘s name will be returned from the query!</p><p>I wasted a bit of time on this and I understand that the situations like this are the reason why it is <a href="https://developer.android.com/training/data-storage/room/testing-db#android">recommended to run database tests on the Android device.</a> On the other hand, on-device tests are obviously much slower and require additional CI setup to run automatically, so we’ll continue to write Robolectric tests for queries it supports, and in rare cases fallback to on-device tests.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=3a93512cf60a" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[A Little Kotlin help on the way to RxJava 2]]></title>
            <link>https://proandroiddev.com/a-little-kotlin-help-on-the-way-to-rxjava-2-6726470fdfff?source=rss-2c408d374281------2</link>
            <guid isPermaLink="false">https://medium.com/p/6726470fdfff</guid>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[software-engineering]]></category>
            <category><![CDATA[kotlin]]></category>
            <category><![CDATA[rxjava]]></category>
            <category><![CDATA[androiddev]]></category>
            <dc:creator><![CDATA[Maciej Witowski]]></dc:creator>
            <pubDate>Thu, 10 Jan 2019 21:29:42 GMT</pubDate>
            <atom:updated>2019-03-23T21:24:54.271Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*IwSskQmT6HVSP3aR" /><figcaption>Photo by <a href="https://unsplash.com/@vidarnm?utm_source=medium&amp;utm_medium=referral">Vidar Nordli-Mathisen</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><h3>A little Kotlin help on the way to RxJava 2</h3><p>Lately, we’ve started migrating the <a href="https://play.google.com/store/apps/details?id=com.futuresimple.base">Zendesk Sell app</a> from RxJava 1 to 2. One of the differences between these releases is that <a href="https://github.com/ReactiveX/RxJava/wiki/What&#39;s-different-in-2.0#nulls">RxJava 2.x no longer accepts </a><a href="https://github.com/ReactiveX/RxJava/wiki/What&#39;s-different-in-2.0#nulls">null values</a>, so using Observable.just(null) results in a NullPointerException.</p><p>Unfortunately, up to this point, we accumulated a lot of code like this, to handle objects not being available somewhere:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/965002b1e9914383cfdf40f2040f626d/href">https://medium.com/media/965002b1e9914383cfdf40f2040f626d/href</a></iframe><p>So what do we do instead of passing null?</p><h3><strong>Introducing Optional</strong></h3><p>The first option which comes to mind is some kind of optional wrapper, like:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/9f9f7eee3668a01fb58b6dcd8896bf3a/href">https://medium.com/media/9f9f7eee3668a01fb58b6dcd8896bf3a/href</a></iframe><p>Applying it to getUserId method, it will now return Observable&lt;Optional&lt;Long&gt;&gt;.</p><h3>Simplify Optional usage</h3><p>Unfortunately, handling Present and Absent types from Optional class adds quite a lot of ceremony.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/27275e69a8687860fdd926959915cd03/href">https://medium.com/media/27275e69a8687860fdd926959915cd03/href</a></iframe><p>Let’s add a map function to simplify it:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/c27782e67c498c900037ad58d369e73c/href">https://medium.com/media/c27782e67c498c900037ad58d369e73c/href</a></iframe><p>This way, using Optional is as easy as using nullable types from the first example.</p><p>Ok, what if the getProfile() function returns an Optional&lt;Profile&gt; instead of a Profile? The type of val profile will become Observable&lt;Optional&lt;Optional&lt;Profile&gt;&gt;&gt;, which isn’t what we wanted.</p><p>You guessed it, we just need another function.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/1c249ae32107e5a19e3186128416819c/href">https://medium.com/media/1c249ae32107e5a19e3186128416819c/href</a></iframe><h3>Handling Observable&lt;Optional&gt;</h3><p>Let’s not kid ourselves though. In many cases getProfile will need to slowly fetch the profile from some place and therefore return Observable&lt;Optional&lt;Profile&gt;&gt;.</p><p>Sadly, this leads us to this less than satisfying piece of code:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/c2509d151fd6bed44c4e2a8022f0bc7f/href">https://medium.com/media/c2509d151fd6bed44c4e2a8022f0bc7f/href</a></iframe><p>The good thing is that after two previous examples, we know we’re just a one method away from more concise code 😄</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/e306556b1ada0116df3c93762fdff94c/href">https://medium.com/media/e306556b1ada0116df3c93762fdff94c/href</a></iframe><p>When we look at one place, using these small and focused functions doesn’t seem to shine, but when applied in dozens of places in the app, they add a pretty good value. The ease of creating such useful functions in Kotlin is something I really like about this language.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=6726470fdfff" width="1" height="1" alt=""><hr><p><a href="https://proandroiddev.com/a-little-kotlin-help-on-the-way-to-rxjava-2-6726470fdfff">A Little Kotlin help on the way to RxJava 2</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[Pitfalls of a foreground Service lifecycle]]></title>
            <link>https://proandroiddev.com/pitfalls-of-a-foreground-service-lifecycle-59f014c6a125?source=rss-2c408d374281------2</link>
            <guid isPermaLink="false">https://medium.com/p/59f014c6a125</guid>
            <category><![CDATA[android-app-development]]></category>
            <category><![CDATA[android]]></category>
            <category><![CDATA[androiddev]]></category>
            <category><![CDATA[mobile-app-development]]></category>
            <category><![CDATA[kotlin]]></category>
            <dc:creator><![CDATA[Maciej Witowski]]></dc:creator>
            <pubDate>Sat, 29 Dec 2018 16:45:47 GMT</pubDate>
            <atom:updated>2019-03-23T21:26:11.264Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*A4W2hmnFLpw3a4po" /><figcaption>Photo by <a href="https://unsplash.com/@shotz?utm_source=medium&amp;utm_medium=referral">Nicolas Cool</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><p>As part of <a href="https://developer.android.com/distribute/best-practices/develop/target-sdk">Google Play’s target API level requirement</a> existing apps need to target at least Android 8.0. One of the migration steps is related to background services: starting a Service when the app is in the background requires calling startForegroundService() instead of startService().</p><p>Docs say that using the former is:</p><blockquote>an implicit promise that the Service will call startForeground(int, android.app.Notification) once it begins running.</blockquote><p>There are a few situations though when fulfilling this promise may surprise us.</p><h3>1. Not calling startForeground() early enough</h3><p>So what happens when startForeground() isn’t called at all?</p><p>Docs again:</p><blockquote>The service is given an amount of time comparable to the ANR interval to do this, otherwise the system will automatically stop the service and declare the app ANR.</blockquote><p>This is only partially true. Only when the app is in the background it behaves as the docs say after the ANR interval and we see:</p><blockquote>ActivityManager: ANR<br>Reason: Context.startForegroundService() did not then call Service.startForeground()</blockquote><p>If the ANR interval passed and the app is in the foreground, it will (after ~10 seconds in my experiments) miserably crash with:</p><blockquote>FATAL EXCEPTION: main<br>android.app.RemoteServiceException: Context.startForegroundService() did not then call Service.startForeground()</blockquote><p>For both of these cases the solution is to c<strong>all </strong><strong>startForeground() at the very beginning of </strong><strong>onCreate()</strong>, to ensure some long-running operation won’t delay it.</p><h3>2. Calling stopSelf() before startForeground()</h3><p>Sometimes we need to stop the Service from within. This can happen when some kind of finishing Intent is received in onStartCommand()<em> </em>or system’s state changes (eg. user logs out and what service does is not relevant anymore). In such cases we call stopSelf(). Calling this method before startForeground() however leads to the same crash as in the previous point.</p><p>The crash still occurs even if startForeground() is actually invoked, like in this purely theoretical scenario:</p><pre>stopSelf()<br>startForeground(ID, notification)</pre><p>Use the solution from point 1 to ensure startForeground() was called before stopSelf().</p><h3>3. Stopping a Service from another place in the app</h3><p>In some scenarios, we may want to stop the Service from another place in the app eg. UI button or some other background operation. The principle of always calling startForeground() before stopping a Service applies here as well.</p><p>However there’s a one tricky case: if some code calls stopService() before Service’s onCreate() callback is invoked, there’s no way to call startForeground() yet and the app will crash.</p><p>As with many lifecycle issues, this one can be tackled with a finite number of boolean flags.</p><p>We can use a helper class like this:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/3d73885631511a80117b928fbe9716fb/href">https://medium.com/media/3d73885631511a80117b928fbe9716fb/href</a></iframe><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/6c9c92e52e7aa97390f820f0fbd88a99/href">https://medium.com/media/6c9c92e52e7aa97390f820f0fbd88a99/href</a></iframe><p>We can then keep a reference to ForegroundServiceLauncher in our Service’s companion object. Our Service’s code looks as follows:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/0c71871739115f21779bd37834efa040/href">https://medium.com/media/0c71871739115f21779bd37834efa040/href</a></iframe><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/79f00b381ea411d64b111be4341f3e5c/href">https://medium.com/media/79f00b381ea411d64b111be4341f3e5c/href</a></iframe><p>The downsides are that we need to remember to always call SampleService.start() and SampleService.stop() methods instead of Context’s startForegroundService() and stopService(). Another is the boilerplate in our Service. Fortunately, there were only few cases when we needed to stop the Service from another place in our app, therefore we decided not to invest time in a better solution.</p><p>As we can see even a seemingly simple contract can lead to subtle issues, which if not developed and tested thoroughly can appear in production.</p><p>If you found similar problems or better solutions to the ones I mentioned, let me know in comments. Thanks!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=59f014c6a125" width="1" height="1" alt=""><hr><p><a href="https://proandroiddev.com/pitfalls-of-a-foreground-service-lifecycle-59f014c6a125">Pitfalls of a foreground Service lifecycle</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>