Skip to content

Snappy1

  • Home
  • Android
  • What
  • How
  • Is
  • Can
  • Does
  • Do
  • Why
  • Are
  • Who
  • Toggle search form

[FIXED] android – RxJava toList() does not trigger subscribe() method (with Room)

Posted on November 11, 2022 By

Solution 1 :

Try to use take(1) instead of toList(), but you should try to not save your results out of stream and do it inside.

EDIT this solution should work as you want:

private fun getData() {
    appDatabase.tierListDao().getChampionOfTier()
        .subscribeOn(Schedulers.io())
        .observeOn(AndroidSchedulers.mainThread())
        .switchMap{ champions ->
            val tierChamps = champions.map { 
                TierChampionAndCounterPicks().apply {
                    tierAndChampion = it
                }
            }

            Observable.fromIterable(tierChamps).switchMap { tierChamp ->
                appDatabase.counterPicksDao()
                    .getCounterPicksWithChampionId(tierChamp.championTable!!.id!!)
                    .subscribeOn(Schedulers.io())
                    .observeOn(AndroidSchedulers.mainThread())
                    .map { counterPicksTableList ->
                        tierChamp.apply {
                            counterPicks = counterPicksTableList
                        }
                    }
            }
        }
        .toList()
        .subscribe({ tierList ->
            tierListAdapter = TierListAdapter(context!!, tierList)
            tierListRv.adapter = tierListAdapter
        }, {
            it.printStackTrace()
        })
}

Problem :

I have a request that I get championList from room database. I just wanna make another request by using championId for every item. So I used Observable.fromIterable(). I have two requests in total and both of them return observable. I will explain my code below:

private fun getData() {
        appDatabase.tierListDao().getChampionOfTier()
            .subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
            .concatMap {
                Observable.fromIterable(it)
            }
            .doOnNext {
                tierListMap[it.championTable!!.id!!] = TierChampionAndCounterPicks().apply {
                    tierAndChampion = it
                }
            }
            .flatMap { tierAndChampion ->
                appDatabase.counterPicksDao()
                    .getCounterPicksWithChampionId(tierAndChampion.championTable!!.id!!)
                    .subscribeOn(Schedulers.io())
                    .observeOn(AndroidSchedulers.mainThread())
            }
            .map {
                tierListMap[it.first().counterPicksTable?.low_level_champion_id]?.apply {
                    counterPicksTableList = it
                }?.let { tier ->
                    tierList.add(tier)
                }
                tierList
            }
            .toList()
            .subscribe({
                tierListAdapter = TierListAdapter(context!!, tierList)
                tierListRv.adapter = tierListAdapter
            }, {
                it.printStackTrace()
            })
    }

I am saving my first result into map with doOnNext. With flatMap, I am making my second request by using championId. I am also saving my second result into map with map() method. After that I wanna trigger subscribe method just once. But without toList() method, subscribe is triggered by the length of my list. With toList() method, subscribe is never triggered. How can I fix it?

Comments

Comment posted by Burcu TOPÇU

When I use take(1), just first item of my list appears on the screen. Without toList() and take(1), everytime subscribe is triggered, new item is added to list. So with take(1) just first item is added to list.

READ  [FIXED] firebase - Unable to receive push notification through expo-notifications in production build Android
Powered by Inline Related Posts

Comment posted by Burcu TOPÇU

Thank you for your edit, it makes my code shorter but again it does not trigger anything after toList(). It is so weird, I think we should write something to complete toList() process but I don’t know what to write. I mean it does not trigger anything after toList() because toList() process is not completed

Comment posted by Nikron

Maybe there is some error on stream. Try to add onError on getCounterPicksWithChampionId and see if everything is ok

Comment posted by Burcu TOPÇU

getCouterPicksWithChampionId works well, it does not enter onError() and it has my list items correctly. So if you have another idea to solve, I can try it also.

Comment posted by Nikron

@BurcuTOPÇU for more help i would need more details of your app maybe it is something wrong with getChampionOfTier() becouse i made my own small sample and everything was ok. Try to debug this place “counterPicks = counterPicksTableList” and look if it runs for every champion from getChampionOfTier().

Android Tags:android, android-room, kotlin, rx-java

Post navigation

Previous Post: [FIXED] Is it possible to switch from Start state to End state in android MotionLayout using Java/Kotlin?
Next Post: [FIXED] android – PHP / SQL: cannot save image at server but successful save image link at SQL database

Related Posts

[FIXED] opengl es – Process HDR10 video on Android with MediaCodec Android
[FIXED] java – Android App: The Cache directory is empty but the device cache indicator shows that it is not empty Android
[FIXED] How pass data from Activity to Dialog in Android Studio Android
[FIXED] java – Could not link static c library with shared lib in android using Cmake Android
[FIXED] android – How to fetch only latest specific data from Firebase? Android
[FIXED] android – How to put Camera component under transparent React Navigator Header? Android

Archives

  • March 2023
  • February 2023
  • January 2023
  • December 2022
  • November 2022
  • October 2022
  • September 2022

Categories

  • ¿Cómo
  • ¿Cuál
  • ¿Cuándo
  • ¿Cuántas
  • ¿Cuánto
  • ¿Qué
  • Android
  • Are
  • At
  • C'est
  • Can
  • Comment
  • Did
  • Do
  • Does
  • Est-ce
  • Est-il
  • For
  • Has
  • Hat
  • How
  • In
  • Is
  • Ist
  • Kann
  • Où
  • Pourquoi
  • Quand
  • Quel
  • Quelle
  • Quelles
  • Quels
  • Qui
  • Should
  • Sind
  • Sollte
  • Uncategorized
  • Wann
  • Warum
  • Was
  • Welche
  • Welchen
  • Welcher
  • Welches
  • Were
  • What
  • What's
  • When
  • Where
  • Which
  • Who
  • Who's
  • Why
  • Wie
  • Will
  • Wird
  • Wo
  • Woher
  • you can create a selvedge edge: You can make the edges of garter stitch more smooth by slipping the first stitch of every row.2022-02-04
  • you really only need to know two patterns: garter stitch

Recent Posts

  • Can VPN be traced by police?
  • Where were Kaiser-Frazer cars built?
  • How do you make gold rose gold paint?
  • What are the newest type of dentures?
  • Can you wear joggers as dress pants?

Recent Comments

No comments to show.

Copyright © 2023 Snappy1.

Powered by PressBook Grid Dark theme