Menu
Who Do Is
  • Home
  • What
  • How
  • Is
  • Can
  • Are
  • Does
  • Do
  • Why
  • Who
  • Where
  • Which
  • Which
  • Should
  • Will
  • When
  • What’s
  • Did
Who Do Is

[ANSWERED] kotlin – Iterator value undefined on first run

Posted on November 14, 2022

Solution 1 :

As both functions populateMyths() and fetchFromDatabase() are launching new coroutines, both these coroutines will run in parallel. So the first time fetchFromDatabase() is called, it may be retrieving the data before dao.insertAll() has happened in populateMyths().
Maybe you should rethink / clarify what you aim to accomplish by launching these coroutines.

Problem :

In my onViewCreated() inside my MythFragment I do these steps.

  1. Prepopulate Room database.
  2. Fetch all records from database into MutableLiveData<List<..>>
  3. Initialize an iterator to these data
  4. Click the “Next” button an check condition it.hasNext() == true

For some reason, only during the first run of the program, the it.hasNext() gives me false. I would expect it to be true, since the steps 1-3 should’ve already ensured, that the list is filled and the iterator points to the first element.

Interestingly, any later navigation on the MythView retrieves the element correctly and it.hasNext() gives me true.

MythFragment.kt

class MythFragment : Fragment() {

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)

        viewModel = ViewModelProviders.of(this).get(MythViewModel::class.java)
        viewModel.populateMyths()
        viewModel.fetchFromDatabase()

        buttonNextMyth.setOnClickListener {
            mythEvaluation.visibility = View.GONE
            buttonIsThisMythTruthful.visibility = View.VISIBLE

            viewModel.mythsIterator.let {
                if (it.hasNext()) {
                    val myth = it.next()
                    mythText.text = myth.myth
                    mythEvaluation.text = myth.evaluation
                } else {
                    Toast.makeText(activity, "There is no myth, because it.hasNext() is false!)", Toast.LENGTH_SHORT).show()
                    val action = MythFragmentDirections.actionMenuFragment()
                    Navigation.findNavController(view).navigate(action)
                }
            }
        }
    }
}

MythViewModel.kt

class MythViewModel(application: Application) : BaseViewModel(application) {

    private val myths = MutableLiveData<List<Myth>>()
    lateinit var mythsIterator: Iterator<Myth>

    fun populateMyths() {
        launch {
            val dao = MythDatabase(getApplication()).mythDao()

            if (dao.getRowCount() > 0)
                [email protected]

            val mythList = arrayListOf(
                Myth("This is the myth 1", "This is the evaluation of the myth 1"),
                Myth("This is the myth 2", "This is the evaluation of the myth 2"),
                Myth("This is the myth 3", "This is the evaluation of the myth 3"),
                Myth("This is the myth 4", "This is the evaluation of the myth 4"),
                Myth("This is the myth 5", "This is the evaluation of the myth 5")
            )

            dao.insertAll(
                *mythList.toTypedArray()
            )
        }
    }

    fun fetchFromDatabase() {
        launch {
            val mythList = MythDatabase(getApplication()).mythDao().getAllMyths()
            myths.value = mythList
            myths.value?.let {
                mythsIterator = it.iterator()
            }
        }
    }
}

I believe the problem might be in the concurrency (Coroutines), but I don’t understand what am I doing wrong.

READ  [ANSWERED] kotlin - How to increase RAM allocated memory in android studio arctic fox?
Powered by Inline Related Posts

Comments

Comment posted by herman

I don’t know yet what may be causing this, but is there any particular reason you are using

Comment posted by herman

BTW you probably don’t even need to use

Comment posted by discuss.kotlinlang.org/t/let-vs-if-not-null/3542

@herman I am just following the practises from a tutorial. Also, I reckoned there is some advatage in

Comment posted by herman

If you check

Comment posted by Slazer

I need to prepopulate my database in

Comment posted by herman

@Slazer what the best place to init DB is, is kindof another question. (can’t help you there). But the thing is, it seems you want to run both methods sequentially. Instead of putting a

Recent Posts

  • How can I play with my cat without toys?
  • What is a bag pipe band called?
  • Are Honda Civics actually fast?
  • Are Yankee candles toxic?
  • How do I pair my Michael Kors smartwatch with my Android?

Recent Comments

No comments to show.

Archives

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

Categories

  • ¿Cómo
  • ¿Cuál
  • ¿Cuántas
  • ¿Cuánto
  • ¿Que
  • ¿Quién
  • 90” and 108” so you may have to round up to the nearest size.
  • and delete any Spotify folders from it. Once this is done
  • Android
  • Are
  • At
  • Bei
  • blink
  • C'est
  • Can
  • carbs
  • Comment
  • Did
  • Do
  • Does
  • During
  • For
  • Has
  • How
  • In
  • Is
  • Ist
  • Kann
  • Können
  • nouveau
  • On
  • or 108 inches.2020-08-03
  • Où
  • owning
  • Pourquoi
  • Puis-je
  • Quand
  • Quante
  • Quel
  • Quelle
  • Quelles
  • Quels
  • Qui
  • Should
  • Sind
  • Sollte
  • spiritual
  • tap the downward-facing arrow on the top left. A downward-facing arrow will appear underneath each song in the album; they'll turn green as the download completes.2020-07-28
  • Uncategorized
  • Wann
  • Warum
  • Was
  • Welche
  • Welcher
  • Welches
  • Welke
  • Wer
  • Were
  • What
  • What's
  • When
  • Where
  • Which
  • Who
  • Whose
  • Why
  • Wie
  • Will
  • Wo
  • Woher
  • you will receive two curtains each with the same measurements of width 66"" (168cm) x drop 54""(137cm).
  • you'll see a green downward-facing arrow next to each song.2021-02-26
©2023 Who Do Is | Powered by SuperbThemes & WordPress