https://kotlinlang.org logo
#flow
Title
# flow
r

Ryan Smith

02/21/2023, 1:35 AM
Does anyone know of examples of (or have any tips regarding) using coroutines + Flow for a simple, local data source in an application? 🧵
I wrote a small Compose multiplatform app that let's you choose directories full of APKs on your machine, shows their absolute paths in a list, and writes a manifest file about them using information from aapt. The app has an
ApkDataRepository
class to manage APK data, and the data source is just a private
MutableStateFlow
property of the repository itself. In trying to adhere to advice from different things I've read, my repository exposes the data as a
Flow
and CRUD methods as suspending functions. IntelliJ is indicating that all of the suspend modifiers on my
ApkDataRepository
functions are redundant so I'm feeling like I did something wrong there. I'm trying out another simple app and would like to learn from my past mistakes, so any advice or examples is appreciated. I've considered that maybe suspending functions and
Flow
are just overkill for such a simple app, but at the same time hope that it's possible to use contrived, low-complexity use cases like this to better understand coroutine and Flow concepts.
c

Chris Fillmore

02/21/2023, 2:18 AM
What do your CRUD operation implementations actually do? Updating a MutableStateFlow via
value
or
update
is a synchronous operation, so suspend is not necessary there. Or do the suspend methods do something else, like work with files/db?
r

Ryan Smith

02/21/2023, 2:39 AM
You're right, they just call
MutableStateFlow.update
. I see now thats not a suspending function... so I suppose by the choice I've made there I've made
suspend
on those functions unnecessary. I guess I could try a data source where CRUD operations work on files.
15 Views