Reputation: 116
I have a table, containing list of cards, in my room db. I have 2 recycler view in my fragment. Need to populate these recycler views basis on card ids. Whenever there is a change in this table, live data gives callback to both the recycler view. Instead I want if that particular card d is updated, only then live data updates my UI. Right now, if any id data is updating, I am getting callback to both the recycler views. For example, if d passed is "Sbi", then if there is a change in sbi data row, only update first recycler view not the second recycler view. Also if passed id is "sbi", then ignore updates in other ids for now.
ACTIVITY
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
myViewModel = ViewModelProviders.of(this).get(MyViewModel.class);
myViewModel.getFirstData("Sbi")).observe(this,
result -> {
myFirstAdapter.addList(result);
});
myViewModel.getSecondData("Sbi1").observe(this,
result -> {
mySecondAdapter.addList(result);
});
}
MyViewModel
public LiveData<List<MyModel>> getFirstData(String cardId) {
return myDao.getItems(cardId);
}
public LiveData<List<MyModel>> getSecondData(String cardDetailId) {
return myDao.getItems(cardDetailId);
}
Upvotes: 0
Views: 65
Reputation: 5103
When you use Room with LiveData - under the hood Room-framework generates some low-level boiler-plate code that is responsible for invoking that magic "callback" when there is some change in table. It looks like a:
mObserver = new InvalidationTracker.Observer(tableNames) {...
According to documentation this method "adds the given observer to the observers list and it will be notified if any table it observes changes". As I understand Room doesn't switch on some "smart-mode" where it analyse parameters of specific query (with "where id:= ..." in your case) and it invokes your observe-LiveData-callbacks whenever table changes, no matter what id you mentioned in your LiveData-function. In other words, if you use Room, you'll give both observe-callbacks with every change in associated sqlite-table.
Upvotes: 1