简体   繁体   中英

Android MVVM: How to subscribe to a network call by LiveData in ViewModel

In MVVM ViewModel generally subscribes to network call and then inform the UI ie ( Activity or Fragment ) for updating the UI like loading, success or failure. In the case of LiveData inside ViewModel , how would you observe network call changes. For observing LiveData inside ViewModel ideally it should be lifecycle aware, how to achieve this. I am using data binding to update UI so I want to listen to results in ViewModel .

您需要在 ViewModel https://developer.android.com/reference/android/arch/lifecycle/Transformations 中使用Transformations.mapTransformations.switchMap函数

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM