RxJava2 Completable Example

When using RxJava to listen to events, especially API responses from Retrofit, we typically use the Observable class. Similarly to using a Single, we don’t always need the onNext.

In fact, sometimes we don’t need any return info example for the complete event. For example, if we are posting a task to an API and we just want to know that the request was successful. For these casses we can use the Completable class.

Say we have this retrofit interface:

public interface APIClient {
    Completable createTask(@Body String taskName);

Then we can simply use a Completable to let us know if the request was successful.

apiClient.createTask("a new task")
    .subscribe(() -> {
        // handle complete
    }, throwable -> {
        // handle error

As with a Single, this helps us save a bit of boilerplate code throughout our code base.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s