Room and Flow!
At next week���s Android Summit, I will be leading a
workshop, ���From RxJava to Coroutines���. The idea is that we would take a small
Retrofit-and-Room app and rewrite it to use coroutines rather than RxJava.
When writing the app, though, I realized that Room did not yet support channels
or flows. The app relied upon Room���s invalidation tracking to deliver fresh results
when the database changed. The only way that I could get a Flow was to have
the DAO use Flowable, then use coroutines��� Reactive Streams support to convert
the Flowable to a Flow. This worked, but it meant that I had leave RxJava
in the app, to get the Flowable.
And so I prayed to Saint Yi��it
that Room would get Flow support in time for the Summit.
Yesterday, my prayers were answered! Room 2.2.0-alpha02 advertised Flow
support, and in light testing, it works as expected! So now your DAO can have
stuff like:
@Query("SELECT * FROM observations ORDER BY timestamp DESC")
abstract fun loadFlow(): Flow<List<ObservationEntity>>
���and you can consume that Flow from within your favorite coroutine builder
(e.g., launch()) or a suspend function.
Of course, not only is this Room version an alpha, but Flow itself is still
in a release candidate state right now. Experimenting with this stuff is fine,
but be careful about shipping production code using this. At least wait until the beta.
But, if you are ���coroutines-curious��� and are attending the Android Summit,
come to my workshop, and I���ll show you how to nuke RxJava from orbit.
UPDATE: Many thanks to Dany Aguacate,
who implemented the Flow support in Room!



