kotlin-flow

How to emit Flow value from different function? Kotlin Coroutines

拥有回忆 提交于 2020-08-09 02:41:56
问题 I have a flow : val myflow = kotlinx.coroutines.flow.flow<Message>{} and want to emit values with function: override suspend fun sendMessage(chat: Chat, message: Message) { myflow.emit(message) } But compiler does not allow me to do this, is there any workarounds to solve this problem? 回答1: The answer of Animesh Sahu is pretty much correct. You can also return a Channel as a flow (see consumeAsFlow or asFlow on a BroadcastChannel). But there is also a thing called StateFlow currently in

Kotlin Coroutines Flow with Room and state handling

谁说我不能喝 提交于 2020-07-23 10:20:16
问题 I'm trying out the new coroutine's flow, my goal is to make a simple repository that can fetch data from a web api and save it to db, also return a flow from the db. I'm using room and firebase as the web api, now everything seems pretty straight forward until i try to pass errors coming from the api to the ui. Since i get a flow from the database which only contains the data and no state, what is the correct approach to give it a state (like loading, content, error) by combining it with the

Kotlin Coroutines Flow with Room and state handling

半腔热情 提交于 2020-07-23 10:16:46
问题 I'm trying out the new coroutine's flow, my goal is to make a simple repository that can fetch data from a web api and save it to db, also return a flow from the db. I'm using room and firebase as the web api, now everything seems pretty straight forward until i try to pass errors coming from the api to the ui. Since i get a flow from the database which only contains the data and no state, what is the correct approach to give it a state (like loading, content, error) by combining it with the

Kotlin Coroutines Flow with Room and state handling

…衆ロ難τιáo~ 提交于 2020-07-23 10:16:09
问题 I'm trying out the new coroutine's flow, my goal is to make a simple repository that can fetch data from a web api and save it to db, also return a flow from the db. I'm using room and firebase as the web api, now everything seems pretty straight forward until i try to pass errors coming from the api to the ui. Since i get a flow from the database which only contains the data and no state, what is the correct approach to give it a state (like loading, content, error) by combining it with the

How can I debounce a setOnClickListener for 1 second using Kotlin Coroutines?

青春壹個敷衍的年華 提交于 2020-07-19 04:47:45
问题 When user taps fast on the button the showDialog() method displays multiple times on top of each other, so when you dismiss it there is another one behind it. I am looking for a way to ignore the second tap for 1 second without using a handler or check the previous tap's time. //Button that opens a dialog button.setOnClickListener { showDialog() } I am looking for a solution using Kotlin coroutines or Kotlin flows for future implementations. 回答1: It's better to use a simple Flag for that

How can I debounce a setOnClickListener for 1 second using Kotlin Coroutines?

不羁岁月 提交于 2020-07-19 04:47:18
问题 When user taps fast on the button the showDialog() method displays multiple times on top of each other, so when you dismiss it there is another one behind it. I am looking for a way to ignore the second tap for 1 second without using a handler or check the previous tap's time. //Button that opens a dialog button.setOnClickListener { showDialog() } I am looking for a solution using Kotlin coroutines or Kotlin flows for future implementations. 回答1: It's better to use a simple Flag for that

How can I debounce a setOnClickListener for 1 second using Kotlin Coroutines?

蹲街弑〆低调 提交于 2020-07-19 04:47:12
问题 When user taps fast on the button the showDialog() method displays multiple times on top of each other, so when you dismiss it there is another one behind it. I am looking for a way to ignore the second tap for 1 second without using a handler or check the previous tap's time. //Button that opens a dialog button.setOnClickListener { showDialog() } I am looking for a solution using Kotlin coroutines or Kotlin flows for future implementations. 回答1: It's better to use a simple Flag for that

How to inject viewModelScope for Android unit test with Kotlin coroutines?

白昼怎懂夜的黑 提交于 2020-06-17 15:47:08
问题 Questions What is the best strategy to inject viewModelScope for Android unit tests with Kotlin coroutines? When the CoroutineScope is injected into a ViewModel for unit tests, should the CoroutineDispatcher also be injected and defined using flowOn even if it is not needed in production code? flowOn is not needed in the production code in this use case as Retrofit handles the threading on Dispatchers.IO in SomeRepository.kt , and the viewModelScope returns the data on Dispathers.Main , both