Reputation: 895
Consider some generic @Entity User
stored in a Room db. Some @Dao
retruns a LiveData<List<User>>
aka 'dao result'. I wish to create another live list where each element is a transform of the dao result, say
LiveData<List<UserWrapper>>
//UserWrapper needs a User to construct
So I write something like
Transformations.map(daoResult,list -> {
List<UserWrapper> newList=new ArrayList<>(list.size());
list.forEach(user -> {
newList.add(new UserWrapper(user));
});
return newList;
});
If my understanding of LiveData is correct, the dao result only responds to List
level changes, say the list is added to, subtracted from etc. and not if the underlying User object is or its fields are changed.
Say some changes are made in the db which have made some new records eligible to be in the result set of the underlying dao result's @Query
LiveData<List<User>>
, the query result, automatically reflect that? Would the Transformations.map
carry it forth to LiveData<List<UserWrapper>>
?In the above implementation every time the underlying list is modified, the map is creating a new List and reinitializing it which to me seems inefficient and wrong somehow. Why should one have to do the manual checking of of both lists to ensure consitent transformed state?
User
s and UserWrapper
s?Upvotes: 0
Views: 86
Reputation: 5103
I'm not sure I've got the point of your question, but consider my thoughts:
select * from users ...
in your query, then EACH change to table users
triggers updating LiveData's value (it's not obvious, so I mean - EACH change, even if some user has been changed that is not in your result list).user
not only List<User>
, but List<UserWrapper>
as well. It depends on the UserWrapper
and User
structures. For that you should write a query with fields that match to UserWrapper
fields, or to use Room's Relations.Upvotes: 1