<@U3ZLHBTLG> I suspect you've dealt with LiveData ...
# android
d
@louiscad I suspect you've dealt with LiveData and Coroutines, did you find a way to do something similar to
Transformations.map { }
using suspend functions in the mapping?
l
@dave08 I didn't do this, but I recall using coroutines in a
LiveData
subclass, and using
switchMap
to instantiate it. Can you tell more about your use case? Maybe there's another simpler way to satisfy it.
d
Basically we get
LiveData
from a Room request and need to make a network call (using a suspend function) to add something to the result before passing it along to the View. @louiscad
l
@dave08 Lastest Room version allows
suspend
functions in DAOs, and if you're stuck to older version for some reason, you can still make your DAO an abstract class, call a protected abstract blocking function and wrap its call using
withContext(<http://Dispatchers.IO|Dispatchers.IO>) { … }
No need for
LiveData
here. If you need self updating features of
LiveData
, you should use
Transformations.switchMap
, and have the
LiveData
doing the network call be instantiated with the value of the other
LiveData
you get from Room.
d
Thanks for the hint of updating Room 🙂. The main problem though is not having room suspend functions, but rather when I receive a list of books in the LiveData it returns (say with pagination, or that this list gets updated in the db throughout the apps lifecycle, so I need room to send this multiple times with the updates), I need to add field contents (like the book rating) by using a suspending network call that does not return LiveData. I just need to hook into the LiveData room is returning, and I think I saw that observing it in the viewmodel is bad practice and Tranformations.map doesn't take suspend functions... switchMap will cut me off from the original room request and not give me db updates AFAIK.
l
Transformations.switchMap does gives you updates, that's the point of it, as I tried to explain in my previous message. See its documentation too.