Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

doOnNext() does not exist in io.reactivex.Single

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 Notes 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);
}
like image 861
Héctor Avatar asked Jan 29 '23 18:01

Héctor


1 Answers

Since a Single only emits one item, it doesn't have a concept of "next" or a doOnNext() operator. Instead use doOnSuccess().

like image 77
Robert Lewis Avatar answered Apr 25 '23 13:04

Robert Lewis