android - Reading two values from datastore flow correctly - Stack Overflow

I have a screen where I wan't to show a loading circle until I readed the default text values for

I have a screen where I wan't to show a loading circle until I readed the default text values for two variables stored in datastore. I'm getting these values in a coroutine in the init block of the viewmodel of the screen. And I update loading to false after the collect of these two flows.

The problem is that for reading these values from the flows returned by datastore, I'm using collect, and collect doesn't end, so the loading circle is never disabled. The idea is to put to false the loading if no variables are on the flows or if one variable is returned by each flow. How to solve this situation?

init {
    viewModelScope.launch(Dispatchers.IO) {
        dataStoreRepository.readString("KEY").collect { value ->
            updateKey(value)
        }

        dataStoreRepository.readString("NAME").collect { value ->
            updateName(value)
        }

        _uiState.update { currentState ->
            currentState.copy(loading = false)
        }
    }
}

I have a screen where I wan't to show a loading circle until I readed the default text values for two variables stored in datastore. I'm getting these values in a coroutine in the init block of the viewmodel of the screen. And I update loading to false after the collect of these two flows.

The problem is that for reading these values from the flows returned by datastore, I'm using collect, and collect doesn't end, so the loading circle is never disabled. The idea is to put to false the loading if no variables are on the flows or if one variable is returned by each flow. How to solve this situation?

init {
    viewModelScope.launch(Dispatchers.IO) {
        dataStoreRepository.readString("KEY").collect { value ->
            updateKey(value)
        }

        dataStoreRepository.readString("NAME").collect { value ->
            updateName(value)
        }

        _uiState.update { currentState ->
            currentState.copy(loading = false)
        }
    }
}
Share Improve this question asked Mar 12 at 12:05 NullPointerExceptionNullPointerException 37.8k80 gold badges231 silver badges405 bronze badges 3
  • Can you not just combine the 2 flows and only emit to _uiState when they both produce values? – Ivan Wooll Commented Mar 12 at 14:12
  • @IvanWooll can you post an answer with your proposal? what will happen if there is no saved value with that proposal? and what if one is returned but other not? – NullPointerException Commented Mar 12 at 14:39
  • Please edit the question to also provide what updateKey and updateName do. You generally don't want to collect flows in the view model, so whatever you do here should be done some other way. Please also provide _uiState and how it is updated elsewhere. – tyg Commented Mar 12 at 17:25
Add a comment  | 

1 Answer 1

Reset to default 0

I would use the combine extension function for this. The below example should hopefully demonstrate how it could be done. Replace keyStringFlow & nameStringFlow with your datastore flows

val keyStringFlow = MutableStateFlow("")
val nameStringFlow = MutableStateFlow("")
val booleanFlow: Flow<Boolean> = keyStringFlowbine(nameStringFlow) { key, name ->
    return@combine key.isNotEmpty() && name.isNotEmpty()
}

init {
    viewModelScope.launch {
        booleanFlow.collect { boolean ->

        }
    }
}

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744753800a4591757.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信