Skip to content

laurentlr/LifeCycleOwnerIssue

Repository files navigation

LifeCycleOwner and ViewModelScope Issue

The viewModelScope is using Dispatchers.Main.immediate since 2.2.0-alpha04
In the case of immediate dispatching (main thread) and couple with a LiveDataOwner only the last MutableLiveData is emitted.

ViewModel

fun initialize() {
        flow {
            //Works with delay(1) or changing dispatcher
            emit("first")
            emit("second")
        }
            .onEach { mutableState.value = it }
            .launchIn(viewModelScope) //Works with Dispatchers.Main

    }

View

myViewModel
           .state
           .observe(this, Observer { //Works when observing for ever
               linear.addView(TextView(this).apply { text = it })
               //only last emitted element is catch
               //Result is: "second"
               //Expected: "first, second"
            })

        myViewModel.initialize()
        

About

LifeCycleOwner and ViewModelScope Issue

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages