Created
December 3, 2018 11:31
-
-
Save anitaa1990/244107a2402bbf8201251ad968d398b8 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* Step 1: Create a subject and emit a | |
* single integer from the subject. (Subject is acting | |
* as an Observable in this case) | |
* */ | |
PublishSubject<Integer> pSubject = PublishSubject.create(); | |
pSubject.onNext(0); | |
/* | |
* Step 2: Subscribe to the Subject - emissions from the subject | |
* will be printed | |
* */ | |
pSubject.subscribe(it -> System.out.println("Observer 1 onNext: " + it), | |
(Throwable onError) -> { }, | |
() -> {}, | |
on1 -> System.out.println("Observer 1 onSubscribe")); | |
/* | |
* Step 3: Again emit values from subject. | |
* */ | |
pSubject.onNext(1); | |
pSubject.onNext(2); | |
/* | |
* Step 4: Again observe the emissions by adding | |
* another Observer to the Subject. | |
* */ | |
pSubject.subscribe(it -> System.out.println("Observer 2 onNext: " + it), | |
(Throwable onError) -> { }, | |
() -> {}, | |
on1 -> System.out.println("Observer 2 onSubscribe")); | |
pSubject.onNext(3); | |
pSubject.onNext(4); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment