I have the following function in my ViewModel:
fun insertMovie(movie: Movie): Long {
var movieId = 0L
viewModelScope.launch {
movieId = repository.insertMovie(movie)
Log.i(LOG_TAG, "add movie with id $movieId within launch")
}
Log.i(LOG_TAG, "add movieId with id $movieId")
return movieId
}
Then the following in the Dao:
@Suppress("RedundantSuspendModifier")
@WorkerThread
suspend fun insertMovie(movie: Movie): Long {
return movieDao.insert(movie)
}
In my activity I am doing the following:
viewModel.viewModelScope.launch {
val movieId = viewModel.insertMovie(Movie(...))
The movieId is always 0. Above I have two logs, the one within the launch{}
shows the right id, but the other shows 0. I don't know how to force the code in my activity to wait until the method completes.