Home > Enterprise >  Getting query result from room database with SharedFlow
Getting query result from room database with SharedFlow

Time:04-01

Hi I just wonder which code snippet is better for scope while getting query result in SharedFlow

val categories = categoryRepository.getAll().shareIn(
    scope = viewModelScope,
    started = SharingStarted.WhileSubscribed(5000),
    replay = 0
)

 val categories = categoryRepository.getAll().shareIn(
    scope = CoroutineScope(Dispatchers.IO),
    started = SharingStarted.WhileSubscribed(5000),
    replay = 0
)

CodePudding user response:

this is better

 val categories = categoryRepository.getAll().shareIn(
        scope = CoroutineScope(Dispatchers.IO),
        started = SharingStarted.WhileSubscribed(5000),
        replay = 0
    )

as viewModelScope is bound to Dispatchers.Main.immediate

  • Related