kotlin-coroutines

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, what is the difference between coroutineScope and withContext

馋奶兔 提交于 2020-08-01 09:57:05
问题 withContext suspend fun <T> withContext( context: CoroutineContext, block: suspend CoroutineScope.() -> T ): T (source) Calls the specified suspending block with a given coroutine context, suspends until it completes, and returns the result. suspend fun <R> coroutineScope( block: suspend CoroutineScope.() -> R ): R (source) Creates a CoroutineScope and calls the specified suspend block with this scope. The provided scope inherits its coroutineContext from the outer scope, but overrides the

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 to pass suspend function as explicit parameter to coroutine builder?

核能气质少年 提交于 2020-07-20 14:43:28
问题 I'm looking into launch coroutine builder which takes coroutine code as block: suspend CoroutineScope.() -> Unit . We usually pass the code as lambda. However, I was wondering how to pass this function as explicit parameter to launch function. coroutineScope { launch(block = ::myFunction) } suspend fun CoroutineScope.myFunction(): Unit { // coroutine code } It gives following error Type mismatch. Required: suspend CoroutineScope.() → Unit Found: KSuspendFunction0<Unit> What is it that i'm

how to pass suspend function as explicit parameter to coroutine builder?

强颜欢笑 提交于 2020-07-20 14:42:20
问题 I'm looking into launch coroutine builder which takes coroutine code as block: suspend CoroutineScope.() -> Unit . We usually pass the code as lambda. However, I was wondering how to pass this function as explicit parameter to launch function. coroutineScope { launch(block = ::myFunction) } suspend fun CoroutineScope.myFunction(): Unit { // coroutine code } It gives following error Type mismatch. Required: suspend CoroutineScope.() → Unit Found: KSuspendFunction0<Unit> What is it that i'm

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