Skip to content

Instantly share code, notes, and snippets.

@anitaa1990
Created December 3, 2018 10:06
Show Gist options
  • Save anitaa1990/6ff85e801e45ff262490f0a44578735a to your computer and use it in GitHub Desktop.
Save anitaa1990/6ff85e801e45ff262490f0a44578735a to your computer and use it in GitHub Desktop.
/*
* Step 1: Create an observable that emits an integer
* from 1 to 5
* */
Observable<Integer> observable = Observable.range(1, 5)
.subscribeOn(Schedulers.io());
/*
* Step 2: Create a subject that observes
* this emission from the observable. In this scenario,
* the subject acts an an observer since it observes the
* changes to the Observable.
* */
ReplaySubject<Integer> subject = ReplaySubject.create();
observable.subscribe(subject);
/*
* Step 3: In this scenario, the subject acts an an Observable,
* since it emits each item from the original Observable.
* */
subject.subscribe(s -> System.out.println("subscriber one: " + s));
subject.subscribe(s -> System.out.println("subscriber two: " + s));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment