Amm*_*mmY 2 android kotlin rx-android android-architecture android-architecture-components
我正在Kotlin中使用Android Archi + Retrofit + RxAndroid。从服务器获得响应时,我需要更新Data对象。但是livedata.addSource的onChanged没有调用。
我正在从Git代码获取帮助:-https: //github.com/shahbazahmed1269/AndroidGithubIssues
这是我在Kotlin中的代码:-
class LoginRepository : BaseRepository() {
fun callLoginApi(data: HashMap<String, String>): LiveData<LoginResponse> {
val liveData: MutableLiveData<LoginResponse> = MutableLiveData<LoginResponse>()
// val call = mApiService.getLoginUser(data)
mApiService.getLoginUser(data)
.subscribeOn(Schedulers.newThread())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(
{ user ->
liveData.value = user
Log.e("response", user.toString())
},
{ error ->
liveData.value = LoginResponse(error = error.localizedMessage)
Log.e("Error", error.message)
})
return liveData
}
}
open class LoginViewModel : ViewModel() {
lateinit var loginResponse : MediatorLiveData<LoginResponse>
lateinit var loginRepo:LoginRepository;
init {
loginResponse = MediatorLiveData<LoginResponse>()
loginRepo = LoginRepository()
}
fun callLoginApi(data: HashMap<String, String>) {
// val loginResponse = MediatorLiveData<LoginResponse>()
loginResponse.addSource(
loginRepo.callLoginApi(data),
{ loginResponse -> Log.e("Response model",loginResponse.toString()) }
)
}
Run Code Online (Sandbox Code Playgroud)
}
我从LoginRepository发出的响应正在打印,但不是来自ViewModel类。
查看addSource()方法的官方文档MediatorLiveData参考文档,其书面内容
仅当此MediatorLiveData处于活动状态时,才会调用onChanged回调。
请确保您正在loginResponse LiveData适当地观察LifecycleOwner类中的。
| 归档时间: |
|
| 查看次数: |
1327 次 |
| 最近记录: |