dave08
07/22/2019, 8:06 AMahulyk
07/22/2019, 8:17 AMgildor
07/22/2019, 9:30 AMRick Busarow
07/22/2019, 2:31 PMonStop()
. If you're using a coroutine to observe a Channel
, then the observer will continue to operate during onPause()
whereas a LiveData
observer would not.
Imagine that your MainActivity
started an observer like this, then it ran in the background for the full lifetime of the app (so long as MainActivity
is in the backstack).
I did a talk just last week on how I handle observing with Channels and coroutines. I talk about one solution to the `CoroutineScope`/lifecycle relationship. You can check it out here: dave08
07/22/2019, 2:41 PMPablichjenkov
07/23/2019, 2:01 AMgildor
07/23/2019, 2:38 AMrisk in losing some eventsWhich is also true for BehaviorSubject if you use it concurrently
Pablichjenkov
07/23/2019, 3:24 PMrkeazor
07/24/2019, 9:03 AMgildor
07/24/2019, 9:04 AMrkeazor
07/24/2019, 9:05 AMgildor
07/24/2019, 9:05 AMrkeazor
07/24/2019, 9:06 AMgildor
07/24/2019, 9:11 AMare ways of communicating via coroutinesNot necessary, but anyway, question was “should they be used side by side”, and if you already have coroutines I see no reason to use LiveData at all, it’s very simple (which is good), but very limited (which is bad) thing and if you use RxJava/Flow/Channels together with LiveData instead of just exposing one of those streams of events you have to redeliver events from one abstraction to another with no real reason
rkeazor
07/24/2019, 9:18 AMgildor
07/24/2019, 9:19 AMLiveData is just a data holder than works with lifecycle awarenessThe same as coroutines with structuredd concurrency
rkeazor
07/24/2019, 9:19 AMgildor
07/24/2019, 9:21 AMrkeazor
07/24/2019, 9:21 AMgildor
07/24/2019, 9:22 AMrkeazor
07/24/2019, 9:26 AMgildor
07/24/2019, 9:26 AMinstance LiveData is a very simple object and very easy to understand implementationI agree with it. I don’t say that if you have only LiveData you should migrate to coroutines or rx, I just say that if you already use coroutines or rxjava, you probably also understand them.
rkeazor
07/24/2019, 9:28 AMgildor
07/24/2019, 9:28 AMrkeazor
07/24/2019, 9:30 AMgildor
07/24/2019, 9:32 AMRxjava also comes with alot of codeagain, returning to original question. If you already have rxjava, this is not a problem, you already have this library
rkeazor
07/24/2019, 9:35 AMgildor
07/24/2019, 9:36 AMrkeazor
07/24/2019, 9:38 AMgildor
07/24/2019, 9:43 AMrkeazor
07/24/2019, 9:54 AMgildor
07/24/2019, 9:58 AMPablichjenkov
07/24/2019, 2:40 PMgildor
07/24/2019, 3:23 PMPablichjenkov
07/24/2019, 3:35 PMgildor
07/24/2019, 3:38 PMPablichjenkov
07/24/2019, 3:45 PMrkeazor
07/26/2019, 12:33 PMPablichjenkov
07/28/2019, 4:15 AMswitch
operator in RxJava by default. It may not be terrible for receiving events in a UI observer, since you don’t want to make un-necessary screen updates and showing the latest is fine.
However, if your observer is not of UI type and is a business logic type of observer, then losing events is not joke.
This fact limits LiveData to only be suitable for UI kind of observers.mData
change in the same Thread where the event is dispatched, then everything become a mess. Apparently they guard against this by using mDispatchingValue
and mDispatchInvalidated
but again will cause the previous event being lost for some observers. No problem for UI observers but big issue for business logic observers.rkeazor
07/28/2019, 11:45 AMPablichjenkov
07/28/2019, 8:14 PM