kotlin-coroutines

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

谁都会走 提交于 2020-12-15 00:48:13
问题 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

Suspending function test with MockWebServer

折月煮酒 提交于 2020-12-13 03:42:49
问题 I'm testing api that returns result using suspending function with MockWebServer, but it does not work with runBlockingTest, testCoroutineDispatcher, testCorounieScope unless a launch builder is used, why? abstract class AbstractPostApiTest { internal lateinit var mockWebServer: MockWebServer private val responseAsString by lazy { getResourceAsText(RESPONSE_JSON_PATH) } @BeforeEach open fun setUp() { mockWebServer = MockWebServer() println("AbstractPostApiTest setUp() $mockWebServer") }

Suspending function test with MockWebServer

梦想的初衷 提交于 2020-12-13 03:41:30
问题 I'm testing api that returns result using suspending function with MockWebServer, but it does not work with runBlockingTest, testCoroutineDispatcher, testCorounieScope unless a launch builder is used, why? abstract class AbstractPostApiTest { internal lateinit var mockWebServer: MockWebServer private val responseAsString by lazy { getResourceAsText(RESPONSE_JSON_PATH) } @BeforeEach open fun setUp() { mockWebServer = MockWebServer() println("AbstractPostApiTest setUp() $mockWebServer") }

io.vertx.mysqlclient.MySQLPool.query (“”).execute is never really executed and return nothing

泄露秘密 提交于 2020-12-13 03:32:36
问题 I am trying a very simple tentative to insert and select to/from MySql using vertx for the first time. It is funny to me that I can build and debug the application but I never get either ar.succeeded or failed. How to execute the insert and select bellow? Do I have to enclose somehow client.query and subscribe it? I am complete stuck. All examples on internet I see follow this approach. package com.mybank import io.vertx.kotlin.mysqlclient.mySQLConnectOptionsOf import io.vertx.kotlin

io.vertx.mysqlclient.MySQLPool.query (“”).execute is never really executed and return nothing

穿精又带淫゛_ 提交于 2020-12-13 03:32:27
问题 I am trying a very simple tentative to insert and select to/from MySql using vertx for the first time. It is funny to me that I can build and debug the application but I never get either ar.succeeded or failed. How to execute the insert and select bellow? Do I have to enclose somehow client.query and subscribe it? I am complete stuck. All examples on internet I see follow this approach. package com.mybank import io.vertx.kotlin.mysqlclient.mySQLConnectOptionsOf import io.vertx.kotlin

How to emit data from an asycnhronous callback using Kotlin Flow?

天大地大妈咪最大 提交于 2020-12-12 06:00:50
问题 I'm starting to learn Kotlin Flow and Coroutines but I do not know how to make the code below works. What am I doing wrong? interface MessagesListener { fun onNewMessageReceived(message: String) } fun messages(): Flow<String> = flow { val messagesListener = object : MessagesListener { override fun onNewMessageReceived(message: String) { // The line below generates the error 'Suspension functions can be called only within coroutine body' emit(message) } } val messagesPublisher =

How to emit data from an asycnhronous callback using Kotlin Flow?

我的梦境 提交于 2020-12-12 05:56:28
问题 I'm starting to learn Kotlin Flow and Coroutines but I do not know how to make the code below works. What am I doing wrong? interface MessagesListener { fun onNewMessageReceived(message: String) } fun messages(): Flow<String> = flow { val messagesListener = object : MessagesListener { override fun onNewMessageReceived(message: String) { // The line below generates the error 'Suspension functions can be called only within coroutine body' emit(message) } } val messagesPublisher =

How to emit data from an asycnhronous callback using Kotlin Flow?

筅森魡賤 提交于 2020-12-12 05:55:26
问题 I'm starting to learn Kotlin Flow and Coroutines but I do not know how to make the code below works. What am I doing wrong? interface MessagesListener { fun onNewMessageReceived(message: String) } fun messages(): Flow<String> = flow { val messagesListener = object : MessagesListener { override fun onNewMessageReceived(message: String) { // The line below generates the error 'Suspension functions can be called only within coroutine body' emit(message) } } val messagesPublisher =

Unit test the new Kotlin coroutine StateFlow

懵懂的女人 提交于 2020-12-05 06:55:49
问题 Recently was introduced the class StateFlow as part of Kotlin coroutine. I'm currently trying it and encounter an issue while trying to unit test my ViewModel. What I want to achieve: testing that my StateFlow is receiving all the state values in the correct order in my ViewModel. My code is as follow: ViewModel: class WalletViewModel(private val getUserWallets: GetUersWallets) : ViewModel() { val userWallet: StateFlow<State<UserWallets>> get() = _userWallets private val _userWallets:

Unit test the new Kotlin coroutine StateFlow

删除回忆录丶 提交于 2020-12-05 06:55:26
问题 Recently was introduced the class StateFlow as part of Kotlin coroutine. I'm currently trying it and encounter an issue while trying to unit test my ViewModel. What I want to achieve: testing that my StateFlow is receiving all the state values in the correct order in my ViewModel. My code is as follow: ViewModel: class WalletViewModel(private val getUserWallets: GetUersWallets) : ViewModel() { val userWallet: StateFlow<State<UserWallets>> get() = _userWallets private val _userWallets: