android-livedata

Observing LiveData from ViewModel

家住魔仙堡 提交于 2019-12-03 05:28:29
问题 I have a separate class in which I handle data fetching (specifically Firebase) and I usually return LiveData objects from it and update them asynchronously. Now I want to have the returned data stored in a ViewModel, but the problem is that in order to get said value, I need to observe the LiveData object returned from my data fetching class. The observe method required a LifecycleOwner object as the first parameter, but I obviously don't have that inside of my ViewModel and I know I am not

MediatorLiveData or switchMap transformation with multiple parameters

爱⌒轻易说出口 提交于 2019-12-03 04:18:45
问题 I am using Transformations.switchMap in my ViewModel so my LiveData collection, observed in my fragment, reacts on changes of code parameter. This works perfectly : public class MyViewModel extends AndroidViewModel { private final LiveData<DayPrices> dayPrices; private final MutableLiveData<String> code = new MutableLiveData<>(); // private final MutableLiveData<Integer> nbDays = new MutableLiveData<>(); private final DBManager dbManager; public MyViewModel(Application application) { super

How to clear LiveData stored value?

帅比萌擦擦* 提交于 2019-12-03 02:38:47
问题 According to LiveData documentation: The LiveData class provides the following advantages: ... Always up to date data: If a Lifecycle starts again (like an activity going back to started state from the back stack) it receives the latest location data (if it didn’t already). But sometimes I don't need this feature. For example, I have following LiveData in ViewModel and Observer in Activity: //LiveData val showDialogLiveData = MutableLiveData<String>() //Activity viewModel.showMessageLiveData

Why LiveData observer is being triggered twice for a newly attached observer

你说的曾经没有我的故事 提交于 2019-12-03 02:36:45
问题 My understanding on LiveData is that, it will trigger observer on the current state change of data, and not a series of history state change of data. Currently, I have a MainFragment , which perform Room write operation, to change non-trashed data , to trashed data . I also another TrashFragment , which observes to trashed data . Consider the following scenario. There are currently 0 trashed data . MainFragment is the current active fragment. TrashFragment is not created yet. MainFragment

Android LiveData - how to reuse the same ViewModel on different activities?

风流意气都作罢 提交于 2019-12-03 02:08:20
Example ViewModel: public class NameViewModel extends ViewModel { // Create a LiveData with a String private MutableLiveData<String> mCurrentName; public MutableLiveData<String> getCurrentName() { if (mCurrentName == null) { mCurrentName = new MutableLiveData<>(); } return mCurrentName; } } Main activity: mModel = ViewModelProviders.of(this).get(NameViewModel.class); // Create the observer which updates the UI. final Observer<String> nameObserver = textView::setText; // Observe the LiveData, passing in this activity as the LifecycleOwner and the observer. mModel.getCurrentName().observe(this,

What is the difference between map() and switchMap() methods?

戏子无情 提交于 2019-12-03 00:57:22
What is the difference between those 2 methods of the LiveData class? The official doc and tutorial are pretty vague on that. In the map() method the first parameter called source but in the switchMap() it called trigger . What's the rationale behind that? As per the documentation Transformations.map() Applies a function on the value stored in the LiveData object, and propagates the result downstream. Transformations.switchMap() Similar to map, applies a function to the value stored in the LiveData object and unwraps and dispatches the result downstream. The function passed to switchMap() must

How to make retrofit API call using ViewModel and LiveData

只愿长相守 提交于 2019-12-02 22:47:51
this is the first time I'm trying to implement MVVM architecture, and I'm a bit confused about the correct way to make an API call. Currently, I'm just trying to make a simple query from the IGDB API, and output the name of the first item in a log. My activity is setup as follow: public class PopularGamesActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_popular_games); PopularGamesViewModel popViewModel = ViewModelProviders.of(this).get(PopularGamesViewModel.class);

Observing LiveData from ViewModel

僤鯓⒐⒋嵵緔 提交于 2019-12-02 21:01:28
I have a separate class in which I handle data fetching (specifically Firebase) and I usually return LiveData objects from it and update them asynchronously. Now I want to have the returned data stored in a ViewModel, but the problem is that in order to get said value, I need to observe the LiveData object returned from my data fetching class. The observe method required a LifecycleOwner object as the first parameter, but I obviously don't have that inside of my ViewModel and I know I am not supposed to keep a reference to the Activity/Fragment inside of the ViewModel. What should I do? In

Notify Observer when item is added to List of LiveData

淺唱寂寞╮ 提交于 2019-12-02 20:12:15
I need to get Observer event when item is added to List of LiveData. But as far as I understand event receives only when I replace the old list with new one. For example when I do the next: list.value = mutableListOf(IssuePost(UserEntity(name, email, photoUrl), issueEntity)) Observer gets event. But when I just add item to value, Observer is silent. Could you please give an advice how I can implement what I need? Internally, LiveData keeps track of each change as a version number (simple counter stored as an int). Calling setValue() increments this version and updates any observers with the

Convert RxJava Observables To Live Data With Kotlin Extension Functions

偶尔善良 提交于 2019-12-02 19:16:15
I've been using alot of RxJava Observables converted to LiveData in my code using LiveDataReactiveStreams.fromPublisher() library. So I though of adding an extension function to the RxJava Observable to easily convert them to LiveData . These are my extension functions: fun <T> Flowable<T>.toLiveData() : LiveData<T> { return LiveDataReactiveStreams.fromPublisher(this) } fun <T> Observable<T>.toLiveData(backPressureStrategy: BackpressureStrategy) : LiveData<T> { return LiveDataReactiveStreams.fromPublisher(this.toFlowable(backPressureStrategy)) } fun <T> Single<T>.toLiveData() : LiveData<T> {