diff --git a/README.md b/README.md index 53ab9af..830d849 100644 --- a/README.md +++ b/README.md @@ -315,6 +315,13 @@ Single.just(ChangeNum(number)) println("------>>${MyRedux.instance.currState().result}") ``` +## Change Logs +#### v1.6.4 +* ViewModel中支持从非主线程中推送数据 + +#### v1.6.3 +* ViewModel支持传入参数 + ## License ```text Copyright 2019 ITGungnir diff --git a/rx_mvvm/src/main/java/my/itgungnir/rxmvvm/core/mvvm/BaseViewModel.kt b/rx_mvvm/src/main/java/my/itgungnir/rxmvvm/core/mvvm/BaseViewModel.kt index 1d73a3c..cb66451 100755 --- a/rx_mvvm/src/main/java/my/itgungnir/rxmvvm/core/mvvm/BaseViewModel.kt +++ b/rx_mvvm/src/main/java/my/itgungnir/rxmvvm/core/mvvm/BaseViewModel.kt @@ -1,5 +1,6 @@ package my.itgungnir.rxmvvm.core.mvvm +import android.os.Looper import androidx.lifecycle.MutableLiveData import androidx.lifecycle.Transformations import androidx.lifecycle.ViewModel @@ -11,7 +12,12 @@ open class BaseViewModel(initialState: T) : ViewModel() { private val state = MutableLiveData().apply { value = initialState } fun setState(reducer: T.() -> T) { - state.value = reducer(state.value!!) + reducer(state.value!!).apply { + when (Looper.getMainLooper() == Looper.myLooper()) { + true -> state.value = this + else -> state.postValue(this) + } + } } fun getState(): T = state.value!!