Skip to content

Instantly share code, notes, and snippets.

@anitaa1990
Created December 3, 2018 11:31
Show Gist options
  • Save anitaa1990/244107a2402bbf8201251ad968d398b8 to your computer and use it in GitHub Desktop.
Save anitaa1990/244107a2402bbf8201251ad968d398b8 to your computer and use it in GitHub Desktop.
/*
* 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