kotlin-coroutines

Kotlin MutableStateFlow.collect is dropping values

我怕爱的太早我们不能终老 提交于 2021-01-05 07:07:29
问题 I have an android app in which I'm trying to use coroutine flows to replace the existing Otto EventBus using my own event bus library. I'm seeing dropped values when setting the MutableStateFlow's value and then collecting in my app's code. I've created a much simpler project that demonstrates the same issue. Any thoughts on why values are being dropped from the MutableStateFlow? Project's build.gradle buildscript { ext.kotlin_version = "1.4.10" repositories { google() jcenter() }

Using Firebase with Kotlin coroutines

情到浓时终转凉″ 提交于 2021-01-03 07:30:03
问题 I am using Kotlin coroutines in my app and have chosen firebase as my choice for database and storage. After exploring firebase I realized that all its APIs are asynchronous and the result of the asynchronous calls are returned in a callback, and getting rid of callbacks is the main reason why I am using Kotlin coroutines in my app. This is the code I have written to upload a file to firebase cloud storage but it is giving "Task is not yet complete" error. private suspend fun saveImage

Using Firebase with Kotlin coroutines

与世无争的帅哥 提交于 2021-01-03 07:29:02
问题 I am using Kotlin coroutines in my app and have chosen firebase as my choice for database and storage. After exploring firebase I realized that all its APIs are asynchronous and the result of the asynchronous calls are returned in a callback, and getting rid of callbacks is the main reason why I am using Kotlin coroutines in my app. This is the code I have written to upload a file to firebase cloud storage but it is giving "Task is not yet complete" error. private suspend fun saveImage

Using Firebase with Kotlin coroutines

|▌冷眼眸甩不掉的悲伤 提交于 2021-01-03 07:28:08
问题 I am using Kotlin coroutines in my app and have chosen firebase as my choice for database and storage. After exploring firebase I realized that all its APIs are asynchronous and the result of the asynchronous calls are returned in a callback, and getting rid of callbacks is the main reason why I am using Kotlin coroutines in my app. This is the code I have written to upload a file to firebase cloud storage but it is giving "Task is not yet complete" error. private suspend fun saveImage

Combine multiple Kotlin flows in a list without waiting for a first value

余生颓废 提交于 2021-01-02 05:47:12
问题 I have a List<Flow<T>> , and would like to generate a Flow<List<T>> . This is almost what combine does - except that combine waits for each and every Flow to emit an initial value, which is not what I want. Take this code for example: val a = flow { repeat(3) { emit("a$it") delay(100) } } val b = flow { repeat(3) { delay(150) emit("b$it") } } val c = flow { delay(400) emit("c") } val flows = listOf(a, b, c) runBlocking { combine(flows) { it.toList() }.collect { println(it) } } With combine

Combine multiple Kotlin flows in a list without waiting for a first value

两盒软妹~` 提交于 2021-01-02 05:46:51
问题 I have a List<Flow<T>> , and would like to generate a Flow<List<T>> . This is almost what combine does - except that combine waits for each and every Flow to emit an initial value, which is not what I want. Take this code for example: val a = flow { repeat(3) { emit("a$it") delay(100) } } val b = flow { repeat(3) { delay(150) emit("b$it") } } val c = flow { delay(400) emit("c") } val flows = listOf(a, b, c) runBlocking { combine(flows) { it.toList() }.collect { println(it) } } With combine

When to use coroutineScope vs supervisorScope?

不羁岁月 提交于 2020-12-29 05:31:25
问题 Can someone explain what exactly is the difference between these two? When do you use one over the other? Thanks in advance. 回答1: The best way to explain the difference is to explain the mechanism of coroutineScope . Consider this code: suspend fun main() = println(compute()) suspend fun compute(): String = coroutineScope { val color = async { delay(60_000); "purple" } val height = async<Double> { delay(100); throw HttpException() } "A %s box %.1f inches tall".format(color.await(), height

When to use coroutineScope vs supervisorScope?

最后都变了- 提交于 2020-12-29 05:31:06
问题 Can someone explain what exactly is the difference between these two? When do you use one over the other? Thanks in advance. 回答1: The best way to explain the difference is to explain the mechanism of coroutineScope . Consider this code: suspend fun main() = println(compute()) suspend fun compute(): String = coroutineScope { val color = async { delay(60_000); "purple" } val height = async<Double> { delay(100); throw HttpException() } "A %s box %.1f inches tall".format(color.await(), height

'Inappropriate blocking method call' - How to handle this warning on Android Studio

我只是一个虾纸丫 提交于 2020-12-15 00:53:16
问题 I have written this code snippet to download image files from firebase storage to local storage. contentResolver.openOutputStream(uri)?.use { ops -> // * Firebase.storage.getReferenceFromUrl(model.mediaUrl).stream.await().stream.use { ips -> val buffer = ByteArray(1024) while (true) { val bytes = ips.read(buffer) // * if (bytes == -1) break ops.write(buffer, 0, bytes) // * } } } In the marked lines, android studio is giving me Inappropriate blocking method call warning, highlighting

'Inappropriate blocking method call' - How to handle this warning on Android Studio

て烟熏妆下的殇ゞ 提交于 2020-12-15 00:48:41
问题 I have written this code snippet to download image files from firebase storage to local storage. contentResolver.openOutputStream(uri)?.use { ops -> // * Firebase.storage.getReferenceFromUrl(model.mediaUrl).stream.await().stream.use { ips -> val buffer = ByteArray(1024) while (true) { val bytes = ips.read(buffer) // * if (bytes == -1) break ops.write(buffer, 0, bytes) // * } } } In the marked lines, android studio is giving me Inappropriate blocking method call warning, highlighting