kotlin-coroutines

Android and Kotlin coroutines: inappropriate blocking method call

与世无争的帅哥 提交于 2020-07-18 09:45:48
问题 I have the following class: class Repository( private val assetManager: AssetManager, private val ioDispatcher: CoroutineDispatcher = Dispatchers.IO ) { suspend fun fetchHeritagesList(): HeritageResponse = withContext(ioDispatcher) { try { // TODO Blocking method call? val bufferReader = assetManager.open("heritages.json").bufferedReader() ... and I'm wondering why do I get a warning in the open("heritages.json") saying Innapropriate blocking method call ? isn't the withContext(ioDispatcher)

Kotlin: Coroutines scope vs Coroutine context

守給你的承諾、 提交于 2020-07-17 07:13:13
问题 Can anyone explain the difference between them? I think scope provides a reference(e.g. Job) to cancel them and context provides a reference to underlying thread. Is that so? 回答1: They are indeed closely related. You might say that CoroutineScope formalizes the way the CoroutineContext is inherited. CoroutineScope has no data on its own, it just holds a CoroutineContext . Its key role is as the implicit receiver of the block you pass to launch , async etc. See this example: runBlocking { val

android LiveData or coroutines channel

非 Y 不嫁゛ 提交于 2020-07-10 09:35:10
问题 Having app using LiveData with ViewModel for UI to observe the data update in the repository. It is working fine. Now someone brought it up the "LiveData has not been well adopted, maybe it should be switch to use coroutines' channel". First of all not sure the statement about LiveData is accurate or not. I am sure with coroutines' feature it could be done without LiveData. But I feel every one has its targeted task, and from the speech/sample of Google the LiveData is built with Android

android LiveData or coroutines channel

空扰寡人 提交于 2020-07-10 09:34:56
问题 Having app using LiveData with ViewModel for UI to observe the data update in the repository. It is working fine. Now someone brought it up the "LiveData has not been well adopted, maybe it should be switch to use coroutines' channel". First of all not sure the statement about LiveData is accurate or not. I am sure with coroutines' feature it could be done without LiveData. But I feel every one has its targeted task, and from the speech/sample of Google the LiveData is built with Android

How to use kotlin coroutines in firebase database

青春壹個敷衍的年華 提交于 2020-07-05 07:32:06
问题 I'm trying to access chatting room Using firestore and coroutines. fun getOwner() { runBlocking { var de = async(Dispatchers.IO) { firestore.collection("Chat").document("cF7DrENgQ4noWjr3SxKX").get() } var result = de.await().result } But i get error like this : E/AndroidRuntime: FATAL EXCEPTION: Timer-0 Process: com.example.map_fetchuser_trest, PID: 19329 java.lang.IllegalStateException: Task is not yet complete at com.google.android.gms.common.internal.Preconditions.checkState(Unknown Source

Combining two listeners kotlin coroutines

牧云@^-^@ 提交于 2020-06-29 04:08:30
问题 I have created a library, in it I have a room database with one Table OfflineData . I listen to new rows inserted in the OfflineData table. I also listen to changes happening in the network connection. I want to combine them both. what I want to listen to a function which returns list of from the table when when there is new row inserted and has internet connection whet the connection comes back on and there is data in the OfflineData table. OfflineDatabaseManager - I have getOfflineData()

Kotlin: coroutineScope is slower than GlobalScope

不羁岁月 提交于 2020-06-27 12:51:48
问题 I'm learning coroutines, and I encounter the following surprising (for me) behavior. I want to have a parallel map. I consider 4 solutions: Just map , no parallelism pmap from here. Modification of item 2: I removed coroutineScope and use GlobalScope . Java's parallelStream . The code: import kotlinx.coroutines.* import kotlin.streams.toList import kotlin.system.measureNanoTime inline fun printTime(msg: String, f: () -> Unit) = println("${msg.padEnd(15)} time: ${measureNanoTime(f) / 1e9}")

Run two Kotlin coroutines inside coroutine in parallel

ぃ、小莉子 提交于 2020-06-27 09:10:08
问题 I have two suspend functions: suspend fun sendData() : Boolean suspend fun awaitAcknowledge() : Boolean and I want to wrap them in a third suspend function in which they should be executed in parallel and I want to calculate the final result by having both return values: suspend fun sendDataAndAwaitAcknowledge() : Boolean { // TODO execute both in parallel and compare both results } However, if I write it like that, suspend fun sendDataAndAwaitAcknowledge() : Boolean { val sendResult =

Handling file download with gRPC on Android

你说的曾经没有我的故事 提交于 2020-06-26 13:53:13
问题 I currently have a gRPC server which is sending chunks of a video file. My android application written in Kotlin uses coroutines for UI updates (on Dispatchers.MAIN) and for handling a unidirectional stream of chunks (on Dispatchers.IO). Like the following: GlobalScope.launch(Dispatchers.Main) { viewModel.downloadUpdated().accept(DOWNLOAD_STATE.DOWNLOADING) // MAKE PROGRESS BAR VISIBLE GlobalScope.launch(Dispatchers.IO) { stub.downloadVideo(request).forEach { file.appendBytes( it.data

Handling file download with gRPC on Android

安稳与你 提交于 2020-06-26 13:53:04
问题 I currently have a gRPC server which is sending chunks of a video file. My android application written in Kotlin uses coroutines for UI updates (on Dispatchers.MAIN) and for handling a unidirectional stream of chunks (on Dispatchers.IO). Like the following: GlobalScope.launch(Dispatchers.Main) { viewModel.downloadUpdated().accept(DOWNLOAD_STATE.DOWNLOADING) // MAKE PROGRESS BAR VISIBLE GlobalScope.launch(Dispatchers.IO) { stub.downloadVideo(request).forEach { file.appendBytes( it.data