Replacing LiveData with SharedFlow

Recently, I was working on refactoring an old module in our app, and had chance to see if using SharedFlow as a replacement for LiveData would be practical. Goal of this article is showcasing few scenarios you can stumble upon, and how coroutines handle them ( hopefully without writing extra code for functionality ).

Some of the cases were not as straightforward as just collecting stuff, so I thought it’d be interesting to share my findings.

P.S. I’ll not focus on collecting / lifecycle awareness in this article.

Overview of ViewModel


I’ve been working with clean architecture approach and using UseCases as interactors for a while now, but most of the time I end up calling corresponding data from required source in it, and return result. If you search and check examples, this is also the case you’ll see in general. Since I’m feeling they are not contributing to logic the way I wanted, I decided to make them more useful.

Let me introduce you what I need from my use case:

  • Load person from persistence first
  • Load person from network after persistence loading ends
  • Update persistence with latest person data


Source: https://wallpaperscraft.com/download/android_os_gray_robot_33642/2560x1440

Hey all ! A little introduction, I’m Mel, I’m working as an Android developer for more than 5 years. I’ve worked with both startups & corporations, and have been involved in development of more or less 20~ apps, some with small user base, some with huge, some with shitty codebase, some with brilliant — nonetheless contributed to my career in long term.

In these 5 years, I’ve switched 6 companies ( one is on the way ). First 4 years of my career I’ve worked in Turkey, 6 months in Latvia, last 6 months ( and ongoing ) in Netherlands…


Well, dependencies, flavors, sourceSets, versions.. All fly around in our build gradle files, right ?

Let’s tidy it up a little bit and make ourselves a helper dependency file which’ll make versioning things around the project easier and make our gradle files look tidier by grouping dependencies.

Let’s not use the root build.gradle file for the extension, instead, create a new folder on the root and name it anything you like. I think scripts would be a suitable name.


Couple years ago, one of my seniors told me that “You can grasp skills of someone by paying attention to their usage of shortcuts”. While you can’t understand if someone can or can’t write some good code with this knowledge, you know over years you found yourself using more and more shortcuts for sake of speed.

So then, let’s begin shall we ? You may know couple of these, but here are some shortcuts I use and example usages.

New

Really ? New ?

Yeah. It’ll make creating new modules / packages / classes faster for you. It’s keymap name is…

Melih Aksoy

Android Engineer @IKEA

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store