Create an Observable that will take arguments

How, if any, to create an Observable capable of accepting parameters?

For example, I can parameterize http requests

+5
source share
1 answer

You can use Observable.create for this:

 public static Observable<String> createMyObservable(final String all, final Integer my, final Boolean parameters) { return new Observable.create(new Observable.OnSubscribe<String>(){ @Override public void call(Subscriber<? super String> subscriber) { // here you have access to all the parameters you passed in and can use them to control the emission of items: subscriber.onNext(all); if (parameters) { subscriber.onError(...); } else { subscriber.onNext(my.toString()); subscriber.onCompleted(); } } }); } 

Please note that all parameters must be declared final or the code will not compile.

If you expect your input parameters to change over time, they can be observable themselves, and you could use combineLatest or zip to combine your values ​​with your other observables, or maybe map or flatMap to create new Observables based on the values your input.

+7
source

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


All Articles