Skip to content

Snappy1

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

[FIXED] android – What is better solution than await with async in coroutines?

Posted on November 11, 2022 By

Solution 1 :

Currently, I think your code is not doing what you think it does. fetchItem is not a suspend function. It launches a coroutine and immediately returns without waiting for the coroutine to finish. So, your async coroutine that calls it doesn’t wait for that result either. There’s no point in using async at all here since the code returns almost immediately.

I’m guessing that what you’re trying to accomplish is wait for postAsyncValue to finish setting that new value. To do this, you need to make fetchItem a suspend function that waits for its work to be done.

I am not familiar with this postAsyncValue, but my best guess is that it is a helper extension function on MutableLiveData that takes a suspend lambda, calls it, and then sets the value to the LiveData on the main thread. If this is the case, you should just do this instead so the function actually waits for the task to be done before returning:

suspend fun fetchItem(id: Int) = withContext(Dispatchers.Main) {
    _item.value = repository.getItem(id) // assuming getItem is a suspend function
}

//...

lifecycleScope.launch {
    viewModel.fetchItem(args.workId)
    onClicked()
}

If this isn’t right, please add your source code for postAsyncValue and let me know.


Regarding your literal question, instead of using async followed immediately by await, you can use withContext(Dispatchers.IO) { }, but you would only do this if you are calling blocking code. You only need async when you’re working with parallelism, so the compiler warns you that you’re doing something silly if you immediately call await() after async { }.

Problem :

I use livedata that collect item data, after it in onClicked() I get this data from livedata. What could be better approach for this?

                lifecycleScope.launch {
                    lifecycleScope.async {
                        viewModel.fetchItem(args.workId)
                    }.await()
                    onClicked()
                }

variables in viewModel

    val item = _item.immutable()
    private val _item = MutableLiveData<ViewState<Item?>>()
[...]
// I wait here for data from first code than I collect it because item is liveData as above
    private fun onClicked() {
        val item = viewModel.item.value?.dataOrNull
[...]

fetchItem method

    fun fetchItem(id: Int) {
        viewModelScope.launch {
            _item.postAsyncValue { repository.getItem(id) }
        }
    }
READ  [FIXED] kotlin - How to save Shared preference after re-installing the app (Android)
Powered by Inline Related Posts
Android Tags:android, android-livedata, asynchronous, kotlin, kotlin-coroutines

Post navigation

Previous Post: [FIXED] Unable to connect Firebase in Android Emulator for Flutter Project
Next Post: [FIXED] android – Highlight only one Card in Compose through selection

Related Posts

[FIXED] android – what is the proublem ? ‘:app:javaPreCompileDebug’ Android
[FIXED] Is it feasible to develop in Android what would be an equivalent of logcat output in little time? Android
[FIXED] javascript – background-color does not work properly on iphone Android
[FIXED] android – Firebase Messaging: Send a data message to all app users with a cloud function Android
[FIXED] android – Adjust toolbar opacity Android
[FIXED] android – connect Flutter code from different sources like for example youtube tutorials 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

  • What is the rising action in Julius Caesar?
  • How do you secure a rope to itself?
  • Does waterproof laminate scratch easily?
  • What makes a building prewar?
  • What can you learn in a month without alcohol?

Recent Comments

No comments to show.

Copyright © 2023 Snappy1.

Powered by PressBook Grid Dark theme