RxJava: Single, Maybe and Completable

Rohit Singh
AndroidPub
Published in
2 min readJan 11, 2018

RxJava2 introduces different types of Observables : Flowable, Single, Maybe and Completable. We’ll be looking into Single, Maybe and Completable in brief.

Observable can emit multiple items

Single, Maybe and Completable are one or no emission of items.

Single

Single is an Observable which only emits one item or throws an error. Single emits only one value and applying some of the operator makes no sense. Like we don’t want to take value and collect it to a list.

interface SingleObserver<T> {
void onSubscribe(Disposable d);
void onSuccess(T value);
void onError(Throwable error);
}

Method onNext() and onComplete() of Observable has been combined to onSucess(), as the stream has only one single item to emit.

Single is like promise in Javascript. A promise is an object that may produce a item or throw an error.

Typically example is a network call, with retrofit you return an Observable or Flowable. You usually care for the response once you can replace this with Single<T>.

public interface APIClient {

@GET("pincode")
Single<City> getCityFromPincode(@Query("pincode") String pincode);
}

How you implement is

apiClient.getCityFromPincode("123456")
.subscribe(city -> {
// handle data fetched successfully and API call completed
},Throwable::printStackTrace);

There are operators that will allow you to turn it into an Observable such as toObservable()

Maybe

Maybe is similar to Single only difference being that it allows for no emissions as well.

interface MaybeObserver<T> {
void onSubscribe(Disposable d);
void onSuccess(T value);
void onError(Throwable error);
void onComplete();
}

We will see example how to implement this

//Some Emission
Maybe<String> singleSource = Maybe.just("single item");

singleSource.subscribe(
s -> System.out.println("Item received: from singleSource " + s),
Throwable::printStackTrace,
() -> System.out.println("Done from SingleSource")
);

//no emission
Maybe<Integer> emptySource = Maybe.empty();
emptySource.subscribe(
s -> System.out.println("Item received: from emptySource" + s),
Throwable::printStackTrace,
() -> System.out.println("Done from EmptySource")
);

As we run the above code snippet

Item received: from singleSource single item
Done from EmptySource
would be printed.

Completable

Completable is only concerned with execution completion whether the task has reach to completion or some error has occurred.

interface CompletableObserver<T> {
void onSubscribe(Disposable d);
void onComplete();
void onError(Throwable error);
}

As Completable only concern is completeness it does not have onNext() and onSucess() method.

Example: There are certain scenario where only concern in completion or error. Suppose we update any User model in the app and want to just notify the server about it. We don’t care about the response because app already has the latest object.

public interface APIClient {

@PUT("user")
Completable updateUser(@Body User);
}

Call the ApiClient updateUser

apiClient.updateUser(user)
.subscribe(() -> {
// handle the completion server has update the user object
},error -> {
//handle error
})

Thanks for reading and if you like the article, remember to clap. Note that if you hold the clap button, you can leave more claps!

--

--