<?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 Cristiano Oliveira on Medium]]></title>
        <description><![CDATA[Stories by Cristiano Oliveira on Medium]]></description>
        <link>https://medium.com/@cristianosoliveira42?source=rss-70119d0b99e7------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*w2AI_SQTbr7-mo_nNJblzQ.jpeg</url>
            <title>Stories by Cristiano Oliveira on Medium</title>
            <link>https://medium.com/@cristianosoliveira42?source=rss-70119d0b99e7------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Mon, 16 Nov 2026 02:45:26 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@cristianosoliveira42/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[Creating an event-driven lib with coroutines]]></title>
            <link>https://medium.com/@cristianosoliveira42/creating-an-event-driven-lib-with-coroutines-bcbc10af85f7?source=rss-70119d0b99e7------2</link>
            <guid isPermaLink="false">https://medium.com/p/bcbc10af85f7</guid>
            <category><![CDATA[androiddev]]></category>
            <category><![CDATA[sdk]]></category>
            <category><![CDATA[kotlin-coroutines]]></category>
            <category><![CDATA[android]]></category>
            <category><![CDATA[kotlin]]></category>
            <dc:creator><![CDATA[Cristiano Oliveira]]></dc:creator>
            <pubDate>Fri, 30 Jul 2021 00:33:58 GMT</pubDate>
            <atom:updated>2021-07-30T00:33:58.153Z</atom:updated>
            <content:encoded><![CDATA[<p><em>Original post </em><a href="https://uiracode.com/event-driven-lib-with-coroutines"><em>here</em></a><em>.</em></p><p>This study note explore a little bit of the asynchronous feature provided by Kotlin, a.k.a <a href="https://kotlinlang.org/docs/coroutines-overview.html">coroutines</a>, by using it on the Android platform.</p><p>Most Android applications work with many asynchronous components, and build consistent communication between them is a way of keeping the application state in control. The idea here is to use coroutines to create a simple lib that handles event broadcasting between any components in the application: an asynchronous message-passing system based on the Event-Driven Architecture (or EventBus).</p><p>This type of architecture is base on the Publisher-Subscriber pattern, having consumers of some event subscribing to receive events and producers publishing events. Each consumer that subscribes to an event will listen to a channel that delivers the event immediately when the producer publishes it.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*GA0JA80_0tHoxZWrRze7Dw.png" /></figure><p>So, first things first, what will be the name of the project? The first thing that pops in my head was “spine”, as an analogy to the spinal column, but I looked in github and already exists a <a href="https://github.com/spine/spine">project</a> with this name. So I just included stream in it and we have the name: SpineStream (SS).</p><p>Before starting to thought about the structure of the project I search for existent libs with the same purpose and I found <a href="https://github.com/greenrobot/EventBus">EventBus</a> (java), <a href="https://github.com/deva666/EventHub">EventHub</a> (java), and <a href="https://github.com/adrielcafe/Broker">Broker</a> (kotlin). I liked the strategy that each one of these took and inspires me to compare to what I wanted to SpineStream.</p><p>One possible source of problems is subscription management, that situation when you forgot to call the function that unsubscribes the channel during the end of the process. All three libs need to call the unsubscription method.</p><p>SpineStream should manage the (un)subscriptions in the background, and those who use just have to call the subscription/publish functions, and the lib makes the necessary control of the channels when the process finishes. So, I wanted to call only one method for each purpose publish/subscribe (plug and play), without managing subscriptions or setting up contexts for the subscription. Here is a spoiler of the final interface:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/fa7615b116e8b19e9b67ba1ab51f1fba/href">https://medium.com/media/fa7615b116e8b19e9b67ba1ab51f1fba/href</a></iframe><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/43b1bf48c82adbed9d9c27e6c46d70de/href">https://medium.com/media/43b1bf48c82adbed9d9c27e6c46d70de/href</a></iframe><p>The requisites for this the initial version are:</p><ul><li>Broadcast events from any component;</li><li>Only have the publish and subscribe interfaces;</li><li>Manage the broadcast channels in the background, to support the workarounds with simple interfaces (no need to deal with channel registration and unsubscription);</li><li>Attach the channel manager to the application lifecycle;</li><li>The events can be any type of data object.</li></ul><p>A way of transfer events with coroutines is by using the broadcast channels primitives. Coroutines provide some different types of broadcast channels and I tested them to make sure which one I going to use in SS. For that, I made a playground test suite on my project to see the behavior of each Broadcast Channel.</p><ul><li><strong>Channel - </strong>have a relation of one to one, meaning that the producer will send the event for one consumer, in the scenario when you have many consumers, the produced event is received by the first consumer and all other ones will not get the event.</li><li><strong>BroadcastChannel - </strong>different from the Channel, the BroadcastChannel assures that all consumers subscribed in the channel receives the event emitted by the producer (one to many relation).</li><li><strong>ConflatedBroadcastChannel - </strong>this one is similar to BroacastChannel, enabling many subscribers, but it assures that the consumers will only receives the most recent event emitted.</li></ul><p>I ended up choosing the BroadcastChannel that delivers all events published, considering the buffer configured, and all subscribers will receive the events. Having the core tool decided, I sketched the following structure for the project.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*9lszQG8fdiSH5T49SkU6lg.png" /></figure><p>The SS architecture are composed by <em>1)</em> an init module, that going to initialize the channels and attach the lib to the application lifecycle; <em>2)</em> A channel manager attached to the application lifecycle responsible for flush the channels when necessary; <em>3)</em> The handler that implements the publisher and subscriber interfaces, containing the logic to exchange events through the channels; <em>4)</em> The SpineStream model makes public the interfaces as a static class.</p><p>In this project, I also wanted to test the initialization using <a href="https://developer.android.com/guide/topics/providers/content-provider-basics">Content Provider</a>. The Android Content Provider is a component that supplies content/data between different applications and can be useful to initialize SDKs. The approach applying content provider eliminates the necessity of calling an init method by the developer who is using the lib. The code below shows the <em>InitProvider</em> that was implemented.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/4ac286d972d7f059b5ff69619d27ef22/href">https://medium.com/media/4ac286d972d7f059b5ff69619d27ef22/href</a></iframe><p>The init module attaches the <em>ChannelsManager</em> to the application lifecycle. The ProcessLifycycleOwner was used to make this binding, which dispatches specific triggers to an observer module regarding lifecycle steps (e.g: ON_START). If you want a more profound understanding of ProcessLifecycleOwner, take a look at the documentation <a href="https://developer.android.com/reference/android/arch/lifecycle/ProcessLifecycleOwner">here</a>. Foreground cycle is mandatory in this first version, so ProcessLifecycleOwner fits well with the purpose because SpineStream just needs to know when the application enters the OnStop state to refresh the in-used channels.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/61901ead0d8238e7e211bdc653f822cd/href">https://medium.com/media/61901ead0d8238e7e211bdc653f822cd/href</a></iframe><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/17dff2a34c3258686d27425af2c152ab/href">https://medium.com/media/17dff2a34c3258686d27425af2c152ab/href</a></iframe><p>The Handler module is the core of SpineStream, which deals directly with the publishing/subscribing operations. To manage different types of data classes, the handler uses a hash map that binds the class name to a specific channel. The coroutines make the events broadcast with BroadcastChannels and uses only background context to make sure that the event exchanges don’t interfere on the mainThread.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/d1d9241780c61769da249f0022264355/href">https://medium.com/media/d1d9241780c61769da249f0022264355/href</a></iframe><p>And that’s pretty much it, the purpose here was to implement an event-driven lib as simple as possible to play with coroutines, as an extra I can evolve this project by increasing other features, or integrate it in another project that fits with these requisites. This project has an overhead implementation by including an init module with the content provider, but it’s something that I wanted to test. There are others types of event-driven implementations for android (or using a publish-subscribe logic, e.g: LiveData), and there are pros and cons of using it, but this discussion isn’t in the scope here. You can look at the SpineStream source code <a href="https://www.github.com/cstiano/spine-stream">here</a>.</p><h4>References</h4><ul><li><a href="https://proandroiddev.com/discovering-event-driven-architecture-for-android-717e6332065e">Discovering Event-Driven Architecture for Android</a></li><li><a href="https://guides.codepath.com/android/creating-content-providers">Creating Content Providers | CodePath Android Cliffnotes</a></li><li><a href="https://www.raywenderlich.com/books/kotlin-coroutines-by-tutorials/v2.0/chapters/12-broadcast-channels">Broadcast Channels</a></li><li><a href="https://medium.com/@arturogdg/background-and-foreground-events-with-android-architecture-components-233fdd9fa855">Background and Foreground events with Android Architecture Components</a></li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=bcbc10af85f7" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>