M Tomczyński M Tomczyński - 1 month ago 15
Android Question

RxJava/RxBinding - check if subscription exists

I'm using RxBinding and creating a subscription in RecyclerView adapter in onBindViewHolder method, which reuses items. Is there any simple way to check if I already assigned subscriber to an EditText object and if so delete that subscription?

My code looks like this

public void onBindViewHolder(final ItemViewHolder holder, final int position) {

holder.text.setText(mProvider.get(position).text);
Subscription textSub = RxTextView.textChanges(holder.text).subscribe(new Action1<CharSequence>() {
@Override
public void call(CharSequence charSequence) {
...
}
});
subscriptions.add(textSub);
}

Answer

Is there anyway to check if I already assigned subscriber to an EditText object and if so delete that subscription?

you could keep it as class member. E.g.

 Subscription textSub = Subscriptions.unsubscribed(); 

and then

public void onBindViewHolder(final ItemViewHolder holder, final int position) {

     holder.text.setText(mProvider.get(position).text);
     textSub.unsubscribe();
     textSub = RxTextView.textChanges(holder.text).subscribe(new Action1<CharSequence>() {
          @Override
          public void call(CharSequence charSequence) {
               ...
          }
      });
}