Hello everyone, I hope you'll are doing great. I h...
# compose
h
Hello everyone, I hope you'll are doing great. I have an issue related to my local db. I configured a room db and there's an issue reading from and writing to it. The issue is generally about coroutines as it reads and writes in it at the same time. To save some time here's the code block that causes the problem:
Copy code
val viewModel: LettersViewModel = koinViewModel()
viewModel.fetchLetters()
val letters = viewModel.letters.collectAsState()
Log.d("Letters is empty", letters.value.isEmpty().toString()
GlobalScope.launch {
    delay(3000L)
    if (letters.value.isEmpty()) {
        var alphabet = ('A'..'Z').map { char ->
            LetterEntity(letter = char.toString())
        }
        viewModel.insertLetters(*alphabet.toTypedArray())
    }
}


class LettersViewModel(private val repository: LetterRepository): ViewModel() {
    private val _letters = MutableStateFlow<List<LetterEntity>>(emptyList())
    val letters: StateFlow<List<LetterEntity>> = _letters

    fun fetchLetters() {
        getAllLetters()
    }

    private fun getAllLetters() {
        viewModelScope.launch {
            _letters.value = repository.getAllLetters().stateIn(viewModelScope).value
        }
    }
    fun insertLetters(vararg letters: LetterEntity) = viewModelScope.launch {
        withContext(<http://Dispatchers.IO|Dispatchers.IO>) {
            repository.insertLetters(*letters)
        }
    }
}


class LetterRepository(private val letterDao: LetterDao) {
    suspend fun getAllLetters(): Flow<List<LetterEntity>> = letterDao.getLetters()
    suspend fun insertLetters(vararg letters: LetterEntity) = letterDao.insertLetters(*letters)
}


@Dao
interface LetterDao {
    @Query("SELECT * FROM letters")
    fun getLetters(): Flow<List<LetterEntity>>

    @Insert
    fun insertLetters(vararg letters: LetterEntity)
}
And also I think that delaying onCreate method is wrong but this is the only way it worked a little) Thank you in advance.
s
Although your database is providing a reactive stream of updates, your viewmodel is pulling it down and reading from it a single time. If you want the UI to reflect the database's state in real-time, you'll need to connect
LetterRepository.getAllLetters()
and
LettersViewModel.letters
s
I have an issue related to my local db
#CJLTWPH7S
You seem to be in the wrong place, no?
h
I just need my app to check at startup if my repository is empty(e.g. the user just installed the app and the local repository needs to be initialized) and set the alphabet in it
s
h
Thanks, I think this will help
z
I think you meant to post in #C1CFAFJSK
h
I didn't have that channel, sorry
Should I post this there as well?
n
could you try Log.d() ?
or you can use async with coroutine and let me know exit error
your issue is reads and writes so check your table
h
There is a Log.d() actually, but my main issue is that I made the delay(3000L) in onCreate method but without that the condition of my if block is true at first even when my repository is not empty so it adds the whole alphabet on top of the existing one before it reads and just with a difference of milliseconds the value of isEmpty turns false from true, but before it does it has already added the alphabet. I had really hard time explaining all this but if there's any additional questions feel free to ask. Thank you!