Questions tagged [kotlin-coroutines]
A language feature and a library introduced in Kotlin 1.3 that is used to implement the "green threads" model. It allows writing non-blocking IO code in a natural way, without resorting to futures or callbacks.
4,533
questions
1
vote
1
answer
17
views
Default Dispatchers use shared threadpool in Coroutines?
After reading the doc on Default Dispatcher -
https://kotlinlang.org/api/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines/-dispatchers/-default.html
It says,
It is backed by a shared ...
1
vote
0
answers
19
views
KTOR MultiPartFormDataContent request is very slow
I need to perform hundreds of thousands multipart requests per hour containing either Form elements or File elements. Each request has 2 parts and the total size of the request is around 30kb but may ...
0
votes
0
answers
11
views
Kotlin OBDII Bluetooth communication: Channel is unrecoverably broken and will be disposed
Im having some issues with retrieving data from my OBDII Adapter. The issue is divided into 2 parts, which maybe are related to each other. I want to mention at the beginning that the issue should not ...
0
votes
1
answer
23
views
What's the best way to delay Splash Screen in Android using Kotlin Coroutines?
I'm using the Splash Screen API along with Kotlin Coroutines to get information from the server. My aim is to make sure the Splash Screen stays visible for at least 3 seconds.
If the server responds ...
4
votes
1
answer
90
views
Why does the Kotlin coroutine output "A B" even with a delay?
I have the following Kotlin code snippets using coroutines, and I'm puzzled by the output. Can someone explain why the output is "A B" in both cases?
// Code Snippet 1
fun main() {
...
0
votes
0
answers
22
views
How to handle mutiple clicks on popBackStack() in jetpack compose
When i click on back icon multiple times it calls navController.popBackStack() that many times and leaves me with a blank screen.
How to deal with it?
Scaffold(
topBar = {
...
0
votes
1
answer
44
views
Call Multiple ViewModel Functions in a Screen vs Single ViewModel Function
Say in the OrderScreen, we have OrderViewModel(orderRepository) and UserViewModel(userRepository).
Approach #1:
class OrderViewModel(private val orderRepository: OrderRepository) : ViewModel() {
...
0
votes
0
answers
11
views
I am trying to get the user by the username in the collection of users, the collections.find() always returns null for the mongodb kotlin drivers
//This is my signin routing function
fun Route.signIn(
userDataSource: UserDataSource,
hashingService: HashingService,
tokenService: TokenService,
tokenConfig: TokenConfig
) {
post(...
0
votes
0
answers
16
views
StateFlow with BufferOverflow.Suspend
I am creating a Manager class which contains its state, InternalError, Running and Idle. This is currently placed in a StateFlow. Issue I have is that I can't lose any values, meaning onBufferOverflow=...
-1
votes
0
answers
25
views
Spring Webflux: CoRouter CoRouterFunctionDsl (Coroutine)-context not applied to onError
I am running web flux with Kotlin coroutines using spring boot 3.2 and the underlying version 6.1.1 of the spring framework.
I am using micrometer for tracing. With spring's coRouter, I followed the ...
0
votes
0
answers
28
views
Reasons to use SupervisorJob over Job in a custom CoroutineScope
I'm trying to understand Kotlin coroutines better, specifically scopes. I created a new project with the following code:
class MainActivity : ComponentActivity() {
companion object {
...
2
votes
1
answer
36
views
How does the Kotlin compiler decide whether a suspend method may be called at any given point?
Kotlin coroutines define delay as a suspending function with the suspend keyword, so
Calling delay as a direct child statement of main is not
allowed, because main is not a suspending function
...
0
votes
0
answers
44
views
Please Help Is there an error for this code in Kotlin? [closed]
fun main() {
val number = 1 // or 2, 3, 4, 5, 6
when (number) {
1 -> {
println("Missed hit")
}
2, 3, 4, 5 -> {
println("Hit ...
1
vote
1
answer
33
views
Emitting on upstream flow inside onSubscription is not collected - despite what documentation says
The documentation for SharedFlow<T>.onSubscription says: (emphasis mine)
Returns a flow that invokes the given action after this shared flow starts to be collected (after the subscription is ...
0
votes
0
answers
26
views
multithreading in kotlin - running some part of a function concurrently
I have this function i want the part were list 'values' is being created to run in threads or concurrently. I want to make sure that the function waits for values list while equating as many fields as ...
0
votes
1
answer
18
views
Retrieving data from DataStore using runBlocking
Would it be a good approach to use runBlocking in following implementation? Would it cause UI to be laggy?
I use it all in my viewModel.
lateinit var centralReturnsPaging: Flow<PagingData<...
0
votes
1
answer
40
views
Invoke Kotlin suspended function inside reactive stream
I use Spring Boot app and Kotlin lang. I want to receive messages though the reactive Kafka consumer, and then pass these messages to the suspended Kotlin function and use coroutines there, then get ...
1
vote
3
answers
156
views
viewModelScope.launch and main thread ANR issue?
As per As per Android Developer doc
viewModelScope.launch Create a new coroutine on the UI thread and code is as below
class LoginViewModel(
private val loginRepository: LoginRepository
): ViewModel()...
0
votes
1
answer
48
views
Kotlin flow : Are there situations when a flow is not the right solution?
Kotlin flow is a relatively new tool added to Kotlin. However, I am not clear about when a Kotlin flow is not the right design choice.
An example I can think of is that of search. Say you have a movie ...
3
votes
0
answers
34
views
What is the purpose of having coroutine-specific functions?
We can't use every and verify with coroutines because they don't take suspend functions as parameters. We have to use coEvery and coVerify instead.
But why can't we use coEvery and coVerify with non-...
0
votes
0
answers
33
views
Coroutines. How to launch parallel coroutine?
I need to make 3 parallel request. And I need an exception for one request not to block or cancel the execution of another coroutine except for only one type of exception.
Here is my code:
...
0
votes
1
answer
53
views
Use "remember" to store a function to cancel coroutine jobs
How can I remember a function in Jetpack Compose? I'd like the search function to return another function that will cancel its two jobs so that when the user hits search again it won't start another ...
2
votes
0
answers
39
views
Subscribing to a hashmap updates using Kotlin Flows
Hi everyone I have been looking at this class
data class Message(val id: String, val text: String)
data class Conversation(val id: String, val messages: List<Message>)
class InMemoryDataSource {
...
1
vote
2
answers
52
views
Returning a value from a Room database using Jetpack Compose
Hello and thank you for considering my question! I'm trying to query a database for a sum and display the total with a Jetpack Compose UI.
My DAO looks like this:
@Dao
interface FindDao {
@Query(...
0
votes
0
answers
29
views
about 'combine' in Kotlin Flow
I encountered some issues when using the combine function provided by the Flow framework to merge two Flows.
Initially, I only needed to observe one event, so I didn't use the combine function. Here's ...
1
vote
1
answer
38
views
Live data builder
I am trying to understand live data builder. From what I have figured out you need to give corutine context, timeout and suspend function to create a live data.
The default value for corutine context ...
0
votes
0
answers
13
views
In Android, Can't observe the Progress of Image uploading in Cloudinary
I want to show the progress of Image uploading in a circular progresbar outside a Image in Jetpack. I convert the callback class to asynchronous.But the problem is no progress is observed in Fragment ...
0
votes
1
answer
31
views
Can't get proper value of variable from suspend function
I am using the shrinkFab function to change the isFabExtended state in Compose. The isFabExtended affects the size of the ExtendableFloatingActionButton.
I want to call shrinkFab anytime the button is ...
1
vote
2
answers
109
views
jOOQ Kotlin Coroutines - Select all and exists queries
I would like to use jOOQ with Kotlin Coroutines but I'm unable to find documentation or example code to know how to use it correctly.
I would like to select all rows that match my query and map them ...
2
votes
2
answers
85
views
StateFlow And LazyColumn recomposition
I have a question related to StateFlow and UI recomposition. In short, my ViewModel has three flows:
accountFlow, which is used to fetch the currently logged-in account from the database.
...
0
votes
0
answers
29
views
How to make multiple asynchronous network calls and upsert them at once?
I have an app with MVVM architecture. DB serves as the single source of truth. If the data is stale, I want to refresh the records by getting latest info from network.
I have to make a separate ...
1
vote
2
answers
61
views
Spring boot kotlin coroutines doesn't run in parallel
I'm building a backend app with spring boot and kotlin. I wanna implement a parallel execution for a specific method. I tried to use coroutines but the underlaying method runs synchronously.
This is ...
0
votes
0
answers
59
views
Why is lifecycleScope not responding in Activity Class?
When I click on an item in the category RecycleView (categoryRecyclerView), which is responsible for the category, the classification code changes.
After that, the goal is to regenerate recyclerView ...
0
votes
0
answers
44
views
Android Media 3 Custom Data Source - Writing bytes to a file with Kotlin Coroutine Channel
Overview
I have a problem in writing bytes in read method Data Source Media 3
My bytes are stored on top of each other something like this (I need write sequentially) I Have Custom Scope with IO ...
0
votes
1
answer
15
views
Why is Maven shade skipping a file from the MCCoroutine library
When running my Spigot plugin (built with Kotlin and Maven) I'm receiving an error about a missing class.
The file 'com.github.shynixn.mccoroutine.bukkit.impl.MCCoroutineImpl' is missing in my .jar. ...
1
vote
0
answers
27
views
Adding Loading States to flatMapLatest in Android with Firebase and Kotlin Coroutines
I am working on an Android project that utilizes Firebase for authentication and data storage. The project follows the MakeItSoApp model from the Firebase documentation and handles errors with ...
4
votes
1
answer
93
views
How do you do a parallel fold on a Kotlin sequence?
As an exercise to help me learn Kotlin, I am writing an application in which I have a large sequence of objects, and I want to find the "best" one according to a set of criteria, for which ...
0
votes
1
answer
70
views
How to remove/update an item from Flow<PagingData<>> in Jetpack compose?
Have a nice day guys.
I am new to jetpack compose and currently don't know how to update an Item in a Flow Paging data. I know that I can update the Item to server and call to get all of them again ...
0
votes
2
answers
81
views
Two methods within a runBlocking coroutine. Second doesn't wait for the first
I have the problem that my saveStore function doesn't wait for the uploadImagesFunction function. The last one loops through an array of multiple images and loads each into Firebase Storage. The ...
0
votes
1
answer
53
views
Kotlin Compose: Data not display async in a lazy column
i have a issue with a lazy column and a database. When i load my activity with a lazy column, the database is call to get list of tournaments. But the list is not display in the lazy column. The list ...
1
vote
0
answers
31
views
OnResume called in quick succession
My app has a crash happening on production, the cause is due to a java.util.ConcurrentModificationException.
In the Breadcrump of the crash log, I noticed that the activities's onResume was called ...
2
votes
1
answer
74
views
How to correctly store data (without db for now) in Android Jetpack Compose Kotlin
Let's say I have a entity for example Dog. I wanna know how to store it's data and if this data changes at some point (using multiple threads) how to make sure it changes in UI as well. May I use ...
2
votes
1
answer
74
views
How can i write unit test for Suspend function and stateflow in android
I am trying to write unit test for my application. I am using Mockk to mock classes.
This is my unit test
@OptIn(ExperimentalCoroutinesApi::class)
class MainViewModalTest {
private lateinit var ...
0
votes
0
answers
30
views
Persistent error: Unresolved reference: runBlocking when using kotlinx-coroutines-core-js:1.7.3
I'm trying to use coroutines and the Arrow library function parMapNotNull to parallelize some code in a scientific computation library I'm working on. Everywhere I read, the function kotlinx....
0
votes
1
answer
48
views
How do I use Kotlin Coroutines to make a database call without freezing the UI?
I am working on a login screen for a compose multiplatform desktop app. Currently I have a button that looks like this
Button(
onClick = {
if (state.validate()) {
...
3
votes
2
answers
104
views
using flowOn to perform an operation in the background and collect in the main thread
I have the following code. And I was thinking that .flowOn(Dispatchers.IO) will perform the map in the BackgroudThread. But the collect will be using the Main thread as that is the thread the ...
3
votes
1
answer
55
views
Is catching Exception an anti-pattern in kotlin coroutines?
Stack traces are not very useful in a coroutine environment, which makes the exception type and message extremely important. Exceptions thrown by third party libraries may have very generic types and ...
0
votes
0
answers
44
views
Kotlin flow that emits only new values to subscribers and keeps it last value in cache
Is it possible to create an equivalent of LiveEvent with Kotlin Flow, i.e. new subscribers receive only new values and the possibility to access the last emitted value?
Similare to
MutableSharedFlow(
...
0
votes
0
answers
46
views
In a unit test, how to verify a function call within a Kotlin coroutine launched in the system under test?
I have a Kotlin Spring Integration TCP/IP endpoint that launches a coroutine:
@MessageEndpoint
class MyTcpEndpoint(private val messageProcessor: MyMessageProcessor) {
@ServiceActivator(...
0
votes
1
answer
54
views
Check that Coroutine Job was cancelled?
I am pretty much newbie with coroutines, I have an existing code and I am trying to understand the following behaviour:
Log.d(TAG,"Starting example function")
val dispatcher = Dispatchers....