Created
June 25, 2019 20:55
-
-
Save carlosgub/1f0c9f1a9de971f53f09a463c613683a to your computer and use it in GitHub Desktop.
Create
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
final List<String> alphabets = getAlphabetList(); | |
/* | |
* Observable.create() -> We will need to call the | |
* respective methods of the emitter such as onNext() | |
* & onComplete() or onError() | |
* | |
* */ | |
Observable observable = Observable.create(new ObservableOnSubscribe() { | |
@Override | |
public void subscribe(ObservableEmitter emitter) { | |
try { | |
/* | |
* The emitter can be used to emit each list item | |
* to the subscriber. | |
* | |
* */ | |
for (String alphabet : alphabets) { | |
emitter.onNext(alphabet); | |
} | |
/* | |
* Once all the items in the list are emitted, | |
* we can call complete stating that no more items | |
* are to be emitted. | |
* | |
* */ | |
emitter.onComplete(); | |
} catch (Exception e) { | |
/* | |
* If an error occurs in the process, | |
* we can call error. | |
* | |
* */ | |
emitter.onError(e); | |
} | |
} | |
}); | |
/* | |
* We create an Observer that is subscribed to Observer. | |
* The only function of the Observer in this scenario is | |
* to print the valeus emitted by the Observer. | |
* | |
* */ | |
Observer observer = new Observer() { | |
@Override | |
public void onSubscribe(Disposable d) { | |
System.out.println("onSubscribe"); | |
} | |
@Override | |
public void onNext(Object o) { | |
System.out.println("onNext: " + o); | |
} | |
@Override | |
public void onError(Throwable e) { | |
System.out.println("onError: " + e.getMessage()); | |
} | |
@Override | |
public void onComplete() { | |
System.out.println("onComplete"); | |
} | |
}; | |
/* | |
* We can call this method to subscribe | |
* the observer to the Observable. | |
* */ | |
observable.subscribe(observer); | |
view rawCreateObservable.java hosted with ❤ by GitHub |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment