0

I am developing an Android app. RxJava is used.

It stores user data in the local database with expire time.

First, it gets user data from the local database. And check the expire time, if the data is old data, then it gets user data from remote server and update it to the local database.

fun getPlayer(playerId: String): Single<Player> {
    return playerDao.getPlayer(playerId)
            .doOnSuccess { // "doOnSuccess" is right? what method should I use?
                if (PlayerUtil.isNeededUpdate(it)) {
                    Log.d(TAG, "getPlayer(local) - old!")
                    getPlayerFromRemote(playerId)
                    // How can I return Observable/Flowable/Single in here?
                    // (case of need to update data)
                }
            }
            .onErrorResumeNext {
                // If local database has no player, then it try to get it from remote server
                Log.d(TAG, "getPlayer(local) - onError: ${it.message}")
                getPlayerFromRemote(playerId)
            }
}
yoonhok
  • 2,575
  • 2
  • 30
  • 58
  • `doOnSuccess` is a side effect operator, not a subscriber, nor does it affect downstream. Consider just contiuning the chain with a `map` or `flatMap` returning something from `getPlayerFromRemote(playerId)` – Mark Jul 20 '18 at 11:58

1 Answers1

2

doOnSuccess is meant to be used for side-effects, not actions that impact the stream itself.

What you're looking for is flatMap and just returning the scalar value if nothing needs to be done:

.flatMap {
    if (PlayerUtil.isNeededUpdate(it)) {
        getPlayerFromRemote(playerId)
    } else {
        Single.just(it)
    }
}
Kiskae
  • 24,655
  • 2
  • 77
  • 74