L
L
Lordao2019-03-14 17:37:54
Kotlin
Lordao, 2019-03-14 17:37:54

How to execute code in doOnNext in rxjava?

From dataSource I receive the list in the form Flowable<List<Item>>from the server. After that, I need to add each element to the local cashes list and then add it to the database. But the code in the second doOnNext is not executed.

return remoteDataSource.loadItem(true).doOnNext {
            Log.d("Log", it.toString())
            caches.clear()
            localDataSource.clearData()
        }.take(1).flatMap { list -> Flowable.fromIterable(list) }.doOnNext {
            Log.d("Log", it.title)
            caches.add(it)
            localDataSource.addItem(it)
        }.toList().toFlowable()

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Denis Zagaevsky, 2019-03-14
@Lordao

You don't have to do that in Rx. doOnNext shouldn't have such powerful side effects. To write to the log - ok, to go to the database - not ok. You need to wrap clear/add in Completable.

remoteDataSource.loadItem(true)
    .flatMap {
         cache.clear().andThen(Observable.just(it))
    }
    .flatMap { list ->
         Observable.fromIterable(list)
            .flatMapCompletable { cache.add(it) }
            .andThen(Observable.just(list))
     }

Somehow, roughly.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question