I'm sorry about the possible off-topic, but I don'...
# android
g
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
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
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
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