Reputation: 453
I have a room DAO with a find method which returns a LiveData.
In my Repository I want call that Dao method and return the LiveData object.
In my ViewModel I call the Repository for LiveData and so on..
My Dao:
@Query("SELECT * FROM user where userId = :userId)
LiveData<User> loadUser(String userId);
Now in my repository I call this Dao, but for userId I have to subscribe on a session.
public LiveData<User> loadUser() {
session.getSubject().subscribe(session -> {
db.getDb().userDao().loadUser(session.userId);
//TODO: How I can return here my LiveData???
});
}
How I could handle such a problem? How I could return this LiveData object in repository method.
Upvotes: 1
Views: 350
Reputation: 3494
Try using MediatorLiveData
like this:
public LiveData<User> loadUser() {
MediatorLiveData<User> userLiveData = new MediatorLiveData<>();
session.getSubject().subscribe(session -> {
userLiveData.addSource(db.getDb().userDao().loadUser(session.userId), user -> {
userLiveData.setValue(user);
});
});
return userLiveData
}
Upvotes: 1