I have been using lately data bindings and i came across the executePendingBindings
method. The documentation shows little much about it and i can't understand how it works or when to use it.
A lot of developers use the executePendingBindings inside the onBindViewHolder callback but i don't see any differences myself in recycler when using it or not.
Can someone explain why its important to use in recycler?? Thanks
@Override public void onBindViewHolder(final RecyclerView.ViewHolder holder, int position) { Customer customer= List.get(position).second; ((CustomerViewHolder)holder).binding.setCustomer (customer) ((CustomerViewHolder)holder).binding.executePendingBindings(); }
Calling executePendingBindings means that you're essentially forcing the framework to do everything it needs to do so far on the binding, right at the moment of calling it. You don't have to do it in your Adapter if your case does not require that.
In order to pass the data to the XML counterpart we bind it using itemRowBinding. setVariable(BR. model, obj); . executePendingBindings() is important in order to execute the data binding immediately.
Doing some changes on your binding does not mean that it will have an immediate effect on your View
. Changing things in binding means that you're really scheduling those changes to be applied in the nearest future. This is for many reasons, performance being one of them.
Imagine you've got some complex expressions in your xmls. You don't want to figure out everything related to binding variables before you set all of them. That would be wasting of resources.
You can see more about it in the generated binding java class itself. I suggest you read through it.
Calling executePendingBindings
means that you're essentially forcing the framework to do everything it needs to do so far on the binding, right at the moment of calling it.
You don't have to do it in your Adapter if your case does not require that. Some people are doing that to be sure that everything is properly set on an item before moving on. So e.g. there's no case like the onBind
being called again before the previous round of bindings was executed... or something similar...
EDIT 1:
Also... don't forget that scheduling of executing changes (for the nearest future) is the thing that allows you to setVariables
on binding
from threads different than UI
thread. Because setting a variable does not touch the View
itself.
EDIT 2:
The easiest way to look at the generated java classes is to:
Binding
(e.g. if your layout is activity_main
, type ActivityMainBinding
)If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With