RxJava2: Alternative to Observable<Void>
Asked Answered
R

2

7

I have an API which returns only error/success codes, with no body. With RxJava1 I would have used Observable<Void> as the return value for this call.

What can I use for RxJava2? The hint on the Wiki for RxJava2 (link) is not helpful, since I can't change how the API works.

Setup:

compile 'com.squareup.retrofit2:retrofit:2.1.0'
compile 'com.squareup.retrofit2:converter-gson:2.1.0'
compile 'com.jakewharton.retrofit:retrofit2-rxjava2-adapter:1.0.0'
Raffarty answered 7/2, 2017 at 15:44 Comment(3)
Have you actually tried to use Observable<Void> ? I use RxJava 2 and Observable<Void>, did not have a single problem.Heterogamete
Observable of Void sends a terminal event, I think.Hobbs
https://mcmap.net/q/545674/-handle-empty-response-with-retrofit-and-rxjava-2-xGoliath
R
25

Use Completable.

If the operation succeeds, it will emit a successful termination event. If it fails, you can use your own Exception subclass to wrap the necessary error codes.

Restrain answered 7/2, 2017 at 16:52 Comment(0)
D
0

You can still use an Observable but replace the Void type with an enum type which is defined like this

public enum Terminal {
    TERMINAL
}

and then use it like this

Observable<Terminal>

PS: You can name the enum anything you want

Dunlop answered 8/4, 2019 at 9:20 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.