RxJava2: Passing Parameter To Observable.create Or fromCallable

August 25, 2017
Use final

There is no special function to pass parameters, just use final.

public Observable<String[]> splitWords(final String phrase) {
    return Observable.fromCallable(new Callable<String[]>() {
        @Override
        public String[] call() throws Exception {
            return phrase.split("\\s+");
        }
    });
}
public Observable<String[]> splitWords(final String phrase) {
    return Observable.create(new ObservableOnSubscribe<String[]>() {
        @Override
        public void subscribe(@NonNull ObservableEmitter<String[]> emitter) throws Exception {
            emitter.onNext(phrase.split("\\s+"));
            emitter.onComplete();
        }
    });
}
This work is licensed under a
Creative Commons Attribution-NonCommercial 4.0 International License.