'Why the array is empty after observer?
I am trying to get the values from the Room database:
val cars = mutableListOf<String>()
carsDb.getAll.observe(viewLifecycleOwner) {
cars = it.toMutableList()
// cars.size = 5
}
// cars.size = 0
Why can't I get the values outside the observer?
I am facing this issuing every time.
Solution 1:[1]
Looks like a problem of synchronisation, the db call carsDb.getAll.observe() is likely asynchronous, so the cars.size=5 you get inside the function is resolved later than the cars.size=0you got just after calling the db call (but before the request is resolved)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Peterrabbit |
