https://kotlinlang.org logo
#android
Title
# android
g

Georgii Goncharik

06/28/2020, 10:10 PM
I'm sorry about the possible off-topic, but I don't know where else to write. Explain why it is returning null, please
Copy code
@Query("SELECT * FROM plant_table WHERE id = :key")
fun get(key: Long): Plant?
j

Javier

06/28/2020, 10:26 PM
Getplantfromdatabase is a suspend function so when you log the value in the init block it is null because it has finished before the suspend function get the value
a

aipok

06/29/2020, 5:26 AM
You should put your log code after
plant.value = getPlantFromDatabase(key)
inside
uiScope.launch
. In this case it will be guaranteed that your suspend function has been completed.
j

Joost Klitsie

06/29/2020, 8:31 AM
Also you shouldn't have to switch to IO context because room will handle the threading for you.
If you make it a suspend function
5 Views