I'm migrating an app from RxJava 1 to 2. I have a code like this:
RxJava 1
public Completable update() {
return client.fetchNotes()
.map(toNote())
.toList()
.doOnNext(save())
.toCompletable();
}
In code above, toList()
returns a Observable<List<Note>>
.
Now, I'm trying to transform that code to RxJava 2:
RxJava 2
public Completable update() {
return client.fetchNotes()
.map(toNote())
.toList()
.doOnNext(save()) // <-- compilation error here
.ignoreElements();
}
In RxJava 2, toList()
returns a Single<List<Note>>
, so I can't chain doOnNext(save())
to it, because doOnNext()
does not exist in Single<T>.class
.
How can I obtain the same behavior in both cases? Basically, in save()
method, I'm storing the Note
s in a database and it has to be from a List<Note>
; Database
interface I'm using does not allow storing one by one. That's why I need to use toList()
:
interface Database {
void store(List<Note> notes);
}
Since a Single
only emits one item, it doesn't have a concept of "next" or a doOnNext()
operator. Instead use doOnSuccess()
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With