RxBindings Sharing Observed Events Between Multiple Subscribers

I use the Focus Observable from the RxBindings library to respond to changes in focus. Since I want to either check input and run the animation, I need focus events twice. Jake Wharton recommends using the share () operator for multiple signatures per observable. But if I manipulate the observable after using share (), the first subscription will be dead.

This is a small example of my use.

public class ShareTest extends AppCompatActivity { @Bind(R.id.editTextOne) EditText editTextOne; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_share_test); ButterKnife.bind(this); Observable<Boolean> focusChangeObservable = RxView.focusChanges(editTextOne); focusChangeObservable.subscribe(focused -> Log.d("test","Subscriber One: "+focused)); focusChangeObservable.share().skip(1).filter(focused -> focused == false).subscribe(focused -> { Log.d("test","Subscriber Two: "+focused); }); } } 

What I needed and expected was the result when I get and lose focus on the EditText,

 Focus gain Subscriber One: true Focus loss Subscriber One: false Subscriber Two: false 

But I really get

 Focus gain Focus loss Subscriber Two: false 

So, it seems that the manipulation of the second case redefines the first. What am I doing wrong and what is the supposed way of exchanging events between multiple subscribers when they manipulate it differently.

thanks

+5
source share
1 answer

RxView.focusChanges () Observable sets a new OnFocusChangeListener in view mode every time you subscribe to it, so the previous listener will not receive any new calls.

In your code, you have two subscriptions - first directly to focusChangeObservable, and the second to the general operator

Turning your observable into a ConnectableObservable via a share statement is a good idea, although you need to subscribe to the Observable returned by share each time fe:

  final Observable<Boolean> sharedFocusChangeObservable = focusChangeObservable.share(); sharedFocusChangeObservable.subscribe(focused -> Log.d("test","Subscriber One: "+focused)); sharedFocusChangeObservable.skip(1).filter(focused -> focused == false).subscribe(focused -> { Log.d("test","Subscriber Two: "+focused); }); 
+7
source

Source: https://habr.com/ru/post/1241392/


All Articles