RxJava Observing on calling/subscribing thread

To answer your question, let me start from beginning, this allows other people to understand what you already know. Schedulers Schedulers play the same role as Executors for Java. Briefly – they decide on which thread actions are executed. Usually an Observable and operators execute in current thread. Sometimes you can pass Scheduler to Observable … Read more

RxJava – fetch every item on the list

Here’s a small self contained example public class Example { public static class Item { int id; } public static void main(String[] args) { getIds() .flatMapIterable(ids -> ids) // Converts your list of ids into an Observable which emits every item in the list .flatMap(Example::getItemObservable) // Calls the method which returns a new Observable<Item> .subscribe(item … Read more

Deliver the first item immediately, ‘debounce’ following items

Update: From @lopar’s comments a better way would be: Observable.from(items).publish(publishedItems -> publishedItems.limit(1).concatWith(publishedItems.skip(1).debounce(1, TimeUnit.SECONDS))) Would something like this work: String[] items = {“one”, “two”, “three”, “four”, “five”}; Observable<String> myObservable = Observable.from(items); Observable.concat( myObservable.first(), myObservable.skip(1).debounce(1, TimeUnit.SECONDS) ).subscribe(s -> System.out.println(s));

Observable which does not pass anything in onNext()

You don’t need to call onNext if your Observable doesn’t emit anything. You could use Void in your signature and do something like Observable<Void> o = Observable.create(new Observable.OnSubscribe<Void>() { @Override public void call(Subscriber<? super Void> subscriber) { // Do the work and call onCompleted when you done, // no need to call onNext if you … Read more

If the onComplete call is made for a RxJava Subject, do i have to manually unsubscribe again?

Subjects are a relatively thin layer on top of an Observable that allow you to feed onNext(), onCompleted() and onError() calls from a source external to the Observable. Their unsubscribe behavior is the same as an Observable. If onCompleted() or onError() are called on the Subject, the subscribers will be unsubscribed. No need to call … Read more

rxjava merge observables of different type

It’s hard to say without knowing exactly what you need, but possibly zip() or combineLatest(). zip will take both Observable<Milk> and Observable<Cereals> and let you combine them into CerealsWithMilk via a provided function. This emits a new CerealsWithMilk each time you get get both a Milk and a Cereals. combineLatest is similar to zip except … Read more

Why is RxJava often used with Retrofit?

Question Retrofit Already in run on background thread. Then why need another background task RxJava? I think most importanly, avoid nested callbacks(callback hell). e.g) Callback hell (Retrofit) public interface MyService { @GET(“users”) Call<List<UserModel>> getUser(); @GET(“userinfo”) Call<UserInfoModel> getUserInfoById(@Query(“id”) Integer id); } service.getUser().enqueue(new Callback<UserModel>() { @Override public void onResponse(Call<UserModel> call, Response<UserModel> response) { //process UserModel UserModel data … Read more

Java Spring WebFlux vs RxJava

Reactive programming is a programming paradigm, but I wouldn’t call it new. It’s actually been around for a while. Just like object-oriented programming, functional programming, or procedural programming, reactive programming is another programming paradigm. Paradigm is defined by Reactive Manifesto Reactive Streams, on the other hand, is a specification. For Java programmers, Reactive Streams is … Read more

Subscribewith Vs subscribe in RxJava2(Android)?

Observable#subscribe explanation: In your first code snippet: .subscribe(this::handleResponse, this::handleError)); You are actually using one of the several overloaded Observable#subscribe methods: public final Disposable subscribe(Consumer<? super T> onNext, Consumer<? super Throwable> onError) There is another one that also takes in an Action to perform onComplete: public final Disposable subscribe(Consumer<? super T> onNext, Consumer<? super Throwable> onError, … Read more