Denis Fedak
Denis Fedak

Reputation: 436

Operator "concat" does not work properly. Kotlin+RxJava

all! I want to get data in DB at first and than call server if DB is empty. But I don't have any response when I use this way. I tried to call server at first and it was successful. Whats wrong??? This is my code:

private fun getDataFromRepository() {
    val subscription =
        carRepository.getCars()!!.
            subscribeOn(Schedulers.io()).
                observeOn(AndroidSchedulers.mainThread()).
            subscribe(
            { cars ->
                LOG.info(cars.size.toString())
                    carRepository.saveCarsInDB(cars)
                data.postValue(cars)
            },
            { e ->
                    loadError.postValue(e.toString())
                    LOG.warning(e.toString())
            })
    subscriptions.add(subscription)
}

Flowables:

fun getCars(): Single<List<Car>>? {
    val db = getDataFromDB()
    val server = getDataFromServerFlowable()
    val mock = getDataFromMock()

    return Flowable.concat(db, server).first(mock)
}

private fun getDataFromServerFlowable(): Flowable<List<Car>> {
    return carApi.getPostsFlowable()
}

private fun getDataFromDB(): Flowable<List<Car>> {
    return RealmCar().queryAllAsFlowable() //"com.github.vicpinm:krealmextensions:2.4.0" 
        .map { cars -> mapper.convertListRealmCarToListCar(cars) }
        .filter { car -> car.isNotEmpty()}
}

private fun getDataFromMock(): List<Car> {
    val cars: MutableList<Car> = mutableListOf()
    val car = Car(0, 0, "Test", "Test", "Test")
    cars.add(car)
    return cars
}

Server call:

@GET("/photos")
fun getPostsFlowable(): Flowable<List<Car>>

Upvotes: 0

Views: 86

Answers (1)

Omar Mainegra
Omar Mainegra

Reputation: 4184

Depending on your logic you should consider using merge instead of concat to interleave the elements. In your case getDataFromDB() is not emitting, so the final Flowable is waiting for it before emitting getDataFromServerFlowable(), There are plenty of good answers of merge vs concat (i.e this one)

Upvotes: 3

Related Questions