icerockdev / moko-mvvm

Model-View-ViewModel architecture components for mobile (android & ios) Kotlin Multiplatform development
https://moko.icerock.dev/
Apache License 2.0
1.02k stars 95 forks source link

Coroutines flow as reactive value holders #39

Closed vchernyshov closed 4 years ago

vchernyshov commented 4 years ago

Why you don't use coroutines flow to implement reactive value holders instead of LiveData?

I mean something like this: State: https://gist.github.com/vchernyshov/d661e153e768915c5a2b3a23a7338b1c ViewModel: https://gist.github.com/vchernyshov/cfd39b16b6e022188b97887f3f4c3351

Alex009 commented 4 years ago

hi! we just using LiveData approach long time, before start use coroutines :) your suggestion is good. I will add it to lib

vchernyshov commented 4 years ago

@Alex009 If I understood correct Flow based approach can replace LiveData and simplify implementation as no need to create separate LiveData class for iOS. I Have tried to modify your sample with State it works but Flow class can't be recognised at Xcode project.

Alex009 commented 4 years ago

what do you mean "Flow class can't be recognised at Xcode"? are you try use export coroutines dependency? https://kotlinlang.org/docs/reference/building-mpp-with-gradle.html#exporting-dependencies-to-binaries Flow as class should be recognised at Xcode. Problem can be only with suspend functions and blocks - they now will be deleted from header of framework

OrhanTozan commented 4 years ago

I think this is relevant: https://github.com/Kotlin/kotlinx.coroutines/pull/1354. Looks like the coroutines team is already busy with a Flow that holds a data.

vchernyshov commented 4 years ago

@NahroTo As I understood it is only lightweight version on channel. And it is a good approach to have wrapper around it, you can change internals and external API will be the same

vchernyshov commented 4 years ago

@Alex009 Maybe it is connected with with suspend function

OrhanTozan commented 4 years ago

Kotlin coroutines 1.3.6 introduces StateFlow, which is a reactive coroutine value holder. I think we can close this issue. Check it out: https://github.com/Kotlin/kotlinx.coroutines/releases/tag/1.3.6

Alex009 commented 4 years ago

as already said - https://github.com/Kotlin/kotlinx.coroutines/releases/tag/1.3.6 have StateFlow with implementation of idea from this issue. issue can be closed