kotlin-coroutines

Difference between usage of Dispatcher IO and Default

落花浮王杯 提交于 2020-05-13 11:38:29
问题 In this question: Kotlin Coroutines choosing Dispatcher we can understand to use Dispatcher.Default on CPU process, like an image/video conversion and Dispatcher.IO when writing/reading files or API connection. But in the class Dispatcher.kt documentation, for the IO you find this: * This dispatcher shares threads with a [Default][Dispatchers.Default] dispatcher, so using * `withContext(Dispatchers.IO) { ... }` does not lead to an actual switching to another thread — * typically execution

How to cancel/unsubscribe from coroutines Flow

谁说我不能喝 提交于 2020-05-11 07:50:21
问题 I notice a strange behavior when trying to prematurely cancel from a Flow. Take a look at the following example. This is a simple flow that emits integer values private fun createFlow() = flow { repeat(10000) { emit(it) } } Then I call the createFlow function using this code CoroutineScope(Dispatchers.Main).launch { createFlow().collect { Log.i("Main", "$it isActive $isActive") if (it == 2) { cancel() } } } This is what is printed out 0 isActive true 1 isActive true 2 isActive true 3 isActive

how to handle callback using kotlin coroutines

て烟熏妆下的殇ゞ 提交于 2020-05-08 04:06:06
问题 the following snippet returns the result as 'null' on sequential code flow. I understand coroutines could be a viable solution to handle the callback asynchronously. fun getUserProperty(path: String): String? { var result: String? = null database.child(KEY_USERS).child(getUid()).child(path) .addListenerForSingleValueEvent(object : ValueEventListener { override fun onCancelled(error: DatabaseError) { Log.e(TAG, "error: $error") } override fun onDataChange(snapshot: DataSnapshot) { Log.w(TAG,

How do I use an async cache with Kotlin coroutines?

谁说胖子不能爱 提交于 2020-03-15 07:28:40
问题 I have a Kotlin JVM server application using coroutines and I need to put a cache in front of a non-blocking network call. I figure I can use a Caffeine AsyncLoadingCache to get the non-blocking cache behaviour I need. The AsyncCacheLoader interface I would need to implement uses CompletableFuture . Meanwhile, the method I want to call to load the cache entries is a suspend function. I can bridge the gap like this: abstract class SuspendingCacheLoader<K, V>: AsyncCacheLoader<K, V> { abstract

How do I use an async cache with Kotlin coroutines?

你说的曾经没有我的故事 提交于 2020-03-15 07:28:10
问题 I have a Kotlin JVM server application using coroutines and I need to put a cache in front of a non-blocking network call. I figure I can use a Caffeine AsyncLoadingCache to get the non-blocking cache behaviour I need. The AsyncCacheLoader interface I would need to implement uses CompletableFuture . Meanwhile, the method I want to call to load the cache entries is a suspend function. I can bridge the gap like this: abstract class SuspendingCacheLoader<K, V>: AsyncCacheLoader<K, V> { abstract

how to pass suspend function as parameter to another function? Kotlin Coroutines

别等时光非礼了梦想. 提交于 2020-03-14 07:07:09
问题 I want to send suspending function as a parameter, but it shows " Modifier 'suspend' is not applicable to 'value parameter" . how to do it? fun MyModel.onBG(suspend bar: () -> Unit) { launch { withContext(Dispatchers.IO) { bar() } } } 回答1: Lambda's suspend modifier should be placed after the colon character, not in front. Example: fun MyModel.onBG(bar: suspend () -> Unit) { launch { withContext(Dispatchers.IO) { bar() } } } 来源: https://stackoverflow.com/questions/55886676/how-to-pass-suspend

how to pass suspend function as parameter to another function? Kotlin Coroutines

醉酒当歌 提交于 2020-03-14 07:06:56
问题 I want to send suspending function as a parameter, but it shows " Modifier 'suspend' is not applicable to 'value parameter" . how to do it? fun MyModel.onBG(suspend bar: () -> Unit) { launch { withContext(Dispatchers.IO) { bar() } } } 回答1: Lambda's suspend modifier should be placed after the colon character, not in front. Example: fun MyModel.onBG(bar: suspend () -> Unit) { launch { withContext(Dispatchers.IO) { bar() } } } 来源: https://stackoverflow.com/questions/55886676/how-to-pass-suspend

Clean way to retain CoroutineScope through config changes without ViewModel

独自空忆成欢 提交于 2020-03-01 06:41:16
问题 I know the recommendation is to use a ViewModel with our Activity, so we can use its viewModelScope . Since the ViewModel outlives the Activity, we don't have to cancel our jobs in activity.onDestroy() . However, sometimes you have a dirt-simple Activity. For example, it could populate a list view with filtered packages that are installed. You can very simply create a scope for the activity using a delegate, and cancel jobs in onDestroy() : class MyActivity(): AppCompatActivity(),

Parallel request with Retrofit, Coroutines and Suspend functions

删除回忆录丶 提交于 2020-02-26 08:32:07
问题 I'm using Retrofit in order to make some network requests. I'm also using the Coroutines in combination with 'suspend' functions. My question is: Is there a way to improve the following code. The idea is to launch multiple requests in parallels and wait for them all to finish before continuing the function. lifecycleScope.launch { try { itemIds.forEach { itemId -> withContext(Dispatchers.IO) { itemById[itemId] = MyService.getItem(itemId) } } } catch (exception: Exception) { exception

Exception not being caught in Coroutines

无人久伴 提交于 2020-02-22 03:19:54
问题 I can't seem to get my error-handling done in coroutines. I've been reading lots of articles and the exception handling documentation but I can't seem to get it working. Here's my setup: My ViewModel launches the coroutine with it's scope class MyViewModel(private var myUseCase: MyUseCase) : ViewModel() { private val viewModelJob = Job() private val uiScope = CoroutineScope(Dispatchers.Main + viewModelJob) fun doSomething() { uiScope.launch { try { myUseCase() } catch (exception: Exception) {