Middle
What are the ways to create observables that you know?
sobes.tech AI
Answer from AI
create(): Allows creating an Observable from scratch, managing data emission manually.just(): Creates an Observable that emits a single item.fromArray(): Creates an Observable from an array of items.fromIterable(): Creates an Observable from any collection implementingIterable.interval(): Creates an Observable that emits a sequence of integers at specified time intervals.timer(): Creates an Observable that emits zero after a specified delay, then completes.range(): Creates an Observable that emits a sequence of integers within a specified range.empty(): Creates an Observable that emits no items and completes immediately.error(): Creates an Observable that immediately emits an error.never(): Creates an Observable that emits no items and never completes.defer(): Creates an Observable that is generated lazily after each subscription.
// Example with just()
Observable<String> singleItemObservable = Observable.just("Hello");
// Example with fromArray()
String[] items = {"A", "B", "C"};
Observable<String> arrayObservable = Observable.fromArray(items);
// Example with create()
Observable<Integer> customObservable = Observable.create(emitter -> {
try {
for (int i = 0; i < 5; i++) {
emitter.onNext(i);
}
emitter.onComplete();
} catch (Exception e) {
emitter.onError(e);
}
});