I implemented a Room DB and have this problem now with my State:
My ViewModel
companion object{
private const val MILLS = 5_000L
}
val allWorkingDaysState : StateFlow<WorkingDayState> = repository.getAllWorkingDays()
.map { WorkingDayState(it) }
.onEach {
println("All Working Days: $it")
}
.stateIn(
scope = viewModelScope,
started = SharingStarted.WhileSubscribed(MILLS),
initialValue = WorkingDayState()
)
fun upsertWorkingDay(workingDay: WorkingDay) {
viewModelScope.launch {
repository.upsertWorkingDay(workingDay)
}
}
My @Composable
val workingDays by viewModelWorkingDays.allWorkingDaysState.collectAsState()
LaunchedEffect(key1 = workingDays) {
Log.d("workingDays", workingDays.toString())
}
The Problem is now as followed:
When I call upsertWorkingDay and change some values. The values get saved in the DB. allWorkingDaysState also emitts a new state. the Log in .onEach is printed.
But here is where the Problem starts. My LaunchedEffect is not triggered.
The other thing is. When I change a value in the DB directly with “App Inspection” in Android Studio, allWorkingDaysState also emitts a new value. the Log in .onEach is printed.
But also my LaunchedEffect is triggered.
Where is the difference. What I am doing wrong? I hope you can help me.
I tried a lot of Log messages. But nothing helped me to find the problem.
I expect that my LaunchedEffect is also triggered if allWorkingDaysState emitts a new value after upsertWorkingDay.
SkAppCoding is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
1