Skip to content

Snappy1

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

[FIXED] kotlin – I am confused with how lifecycleScope in android studio works

Posted on November 11, 2022 By

Solution 1 :

lifecycleScope.launch will start a coroutine, to make it simple the code inside lifecycleScope.launch will be executed in another thread and it will take some time until inserting data and reading it from database, but println(a) is on the main thread so it will be executed before this line a = it[it.size – 1].name, so your println(a) should be inside lifecycleScope.launch like this:

class MainActivity : AppCompatActivity() {
    private var binding: ActivityMainBinding? = null
    private var a: String = "Hi"

    override fun onCreate(savedInstanceState: Bundle?) {
        binding = ActivityMainBinding.inflate(layoutInflater)
        super.onCreate(savedInstanceState)
        setContentView(binding?.root)

        val somethingDao = SomethingDatabase.getDatabase(this).somethingDao()

        lifecycleScope.launch {
            somethingDao.insert(SomethingModel("Hello"))
            somethingDao.fetchAllSomething().collect {
                a = it[it.size - 1].name
                println(a)
            }
        }
    }
}

Note: take a look on kotlin coroutines to better understand

Problem :

I am confused about how flow.collect works. Because in the lifecycleScope below I already say that a should be assigned by the value of data in my database. However, the value of a is still the string of “Hi” instead of “Hello”.

class MainActivity : AppCompatActivity() {
    private var binding: ActivityMainBinding? = null
    private var a: String = "Hi"

override fun onCreate(savedInstanceState: Bundle?) {
    binding = ActivityMainBinding.inflate(layoutInflater)
    super.onCreate(savedInstanceState)
    setContentView(binding?.root)

    val somethingDao = SomethingDatabase.getDatabase(this).somethingDao()

    lifecycleScope.launch {
        somethingDao.insert(SomethingModel("Hello"))
        somethingDao.fetchAllSomething().collect {
            a = it[it.size - 1].name
        }
    }

    println(a)

}
}

this is all of the information in my database

enter image description here

Comments

Comment posted by Sanchez Schofield

Thanks @Mohamed Rejeb. Anyways, can we change the data of the variable a in lifecycleScope? Because the only way to get the data and assign it to the variable a is within lifecycleScope which does not affect the data on main thread. I just want to find the way to change the data in lifecycleScope and might as well change the value of variable a on main thread

Comment posted by Mo Coding

The best practice is to change data and get it inside the lifecycleScope, you can do anything with the data inside the lifecycleScope

READ  [FIXED] sockets - Implementing Stories feature in Android
Powered by Inline Related Posts

Android Tags:android, kotlin, kotlin-flow

Post navigation

Previous Post: [FIXED] java – is permission needed when choosing image from gallery on android?
Next Post: [FIXED] Android Java: How to get component in a custom view? (Trying to access components via findByViewId and get null)

Related Posts

[FIXED] android – is it possible to use adb on an unauthorized device? Android
[FIXED] java – Cant convert String to double Android
[FIXED] android – How to exclude a property only if it’s null in Firestore? Android
[FIXED] android – Error while using image picker and web_image_picker in flutter Android
[FIXED] ProcessCameraProvider.BindToLifecycle Using Xamarin.Android Activity Instead of AppCompatActivity Android
[FIXED] java – How can I rotate an AR object in place? 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