0
votes

I am using Room in a project and I have the following DAO interface:

@Dao
interface BalanceDao {
    @Query("SELECT * FROM balance")
    fun getAllBalances(): Flowable<List<BalanceDataModel>>

    @Insert(onConflict = OnConflictStrategy.REPLACE)
    fun insertBalanceList(balanceDataModelList: List<BalanceDataModel>): List<Long>
}

Insert works fantastically, but the getAllBalances() method does not work, since it does not retrieve any row. I extracted the DB after the insertion and I can see all the rows there; SELECT * from balance works perfectly when locally executed to the extracted DB with a desktop app. I also tried to change the return type of getAllBalances() from Flowable<List<BalanceDataModel>> to Single<List<BalanceDataModel>> but the same keeps happening: no results.

I have a PortfolioManager, from which I call the following method and I pass the observer and the owner from my Fragment.

fun getAllCoins(owner: LifecycleOwner, observer: Observer<List<Balance>>) {
        portfolioViewModel
            .balanceListLiveData
            .observe(owner, observer)
        return portfolioViewModel.getPortfolioCoins()
    }

Then in the PortfolioManager, I have access to a ViewModel, from which I call the following method:

fun getPortfolioCoins() {
        coinRepository
                .getBalanceListPortfolio()
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeBy(onSuccess = {
                    balanceListLiveData.postValue(it)
                })
    }

And in the repository class, I have this:

fun getBalanceListPortfolio(): Single<List<Balance>> {
        val converter = BalanceDataModelConverter()
        val balanceList = mutableListOf<Balance>()
        coinDatabase.balanceDao()
                .getAllBalances()
                .toObservable()
                .flatMapIterable { t: List<BalanceDataModel> -> t }
                .map { t: BalanceDataModel ->
                    {
                        val a: Balance = converter.fromFirstToSecond(t)
                        balanceList.add(a)
                    }
                }
        return Single.just(balanceList.toList())
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
    }

Anybody knows what could be wrong? Thanks a lot in advance!

1
How are you subscribing to the observable? Can you show the code? - tompee
thanks @tompee updated the question with more details - noloman
Did you try setting a breakpoint at the onSuccess in getPortfolioCoins and verify if nothing is indeed coming out of the stream? I suspect that the issue occurs somewhere outside the rx stream already. - tompee
I suspect that the balance list was indeed posted in balanceListLiveData but since no observer is configured yet, it is not published. Try setting an observer to the live data first before subscribing to the rx observable. - tompee
I also suspected that (or at least that was my hope), but I have tried that (I updated the getAllCoins() method in the question) but still, the same - noloman

1 Answers

1
votes

I just saw your recent edit. I think your problem is you are returning an empty list in getBalanceListPortfolio. This observable was not subscribed upon.

coinDatabase.balanceDao()
            .getAllBalances()
            .toObservable()
            .flatMapIterable { t: List<BalanceDataModel> -> t }
            .map { t: BalanceDataModel ->
                {
                    val a: Balance = converter.fromFirstToSecond(t)
                    balanceList.add(a)
                }
            }

I suggest you convert this to list and return this observable (something like this, cant try to compile. I have no working env right now).

return coinDatabase.balanceDao()
            .getAllBalances()
            .toObservable()
            .flatMapIterable { t: List<BalanceDataModel> -> t }
            .map { t: BalanceDataModel -> converter.fromFirstToSecond(t) }
            .toList()
            .toObservable()
            .subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())

This is wrapped as an observable to you might want to change your type to Observable (instead of single) first, just to test. Let me know the results.