Questions tagged [kotlinx.coroutines]

Library support for Kotlin coroutines in Kotlin/JVM and Kotlin/JS

296 questions
44
votes
3 answers

Suspend function 'callGetApi' should be called only from a coroutine or another suspend function

I am calling suspended function from onCreate(...) override fun onCreate(savedInstanceState: Bundle?) { ... ... callGetApi() } and the suspended function is:- suspend fun callGetApi() {....} But the error shows up Suspend function…
Santanu Sur
  • 8,836
  • 7
  • 24
  • 43
36
votes
6 answers

Kotlin Coroutines with returning value

I want to create a coroutine method which has returning value. For example) fun funA() = async(CommonPool) { return 1 } fun funB() = async(CommonPool) { return 2 } fun sum() { launch { val total = funA().await() +…
Expert wanna be
  • 8,490
  • 21
  • 98
  • 148
34
votes
9 answers

Module with Main dispatcher is missing

I'm trying to make a background call to my local database and update the UI with the results using coroutines. Here is my relevant code: import kotlinx.coroutines.experimental.* import kotlinx.coroutines.experimental.Dispatchers.IO import…
Khongor Bayarsaikhan
  • 1,266
  • 1
  • 10
  • 16
31
votes
11 answers

Kotlin Android debounce

Is there any fancy way to implement debounce logic with Kotlin Android? I'm not using Rx in project. There is a way in Java, but it is too big as for me here.
28
votes
3 answers

Why not use GlobalScope.launch?

I read that usage of Globalscope is highly discouraged, here. I have a simple use-case. For every kafka message (let's say a list of Ids) that I receive I have to split it and invoke a rest service simultaneously and wait for it to be done and…
so-random-dude
  • 12,394
  • 9
  • 47
  • 96
18
votes
2 answers

Using kotlinx.coroutines in IntelliJ IDEA project

I am trying to learn coroutines and so I fire up IntelliJ and create a scratch file. But when I type in my coroutines I get compiler complaints such as runBlocking is an unresolved reference. So this is not an android project or any such thing. Just…
salyela
  • 757
  • 2
  • 7
  • 18
17
votes
3 answers

Room with Kotlin-coroutines observe db changes

So, I recently started experimentation with coroutines, I switched from Rxjava2 to coroutines, I haven't got a grasp of it yet but still, I ran into a condition where I needed to observe my database change and update the UI corresponding to…
Rajat Beck
  • 1,233
  • 10
  • 24
15
votes
2 answers

How to launch a Kotlin coroutine in a `suspend fun` that uses the current parent Scope?

How can I launch a coroutine from a suspend function and have it use the current Scope? (so that the Scope doesn't end until the launched coroutine also ends) I'd like to write something like the following – import kotlinx.coroutines.* fun main() =…
Rusty Fieldstone
  • 633
  • 5
  • 15
15
votes
4 answers

How to transform an Android Task to a Kotlin Deferred?

Firebase anonymous sign in returns a task (which is basically Google promise implementation): val task:Task = FirebaseAuth.getInstance().signInAnonymously() How it would be possible create a signInAnonymous wrapper where: It is a…
JP Ventura
  • 4,663
  • 5
  • 43
  • 62
15
votes
3 answers

Kotlin Coroutines in a Android Service

I have a android service which starts and syncs different types of data with the server when it's online. I'm new to Kotlin coroutines and I'm trying to accomplish the following: fun syncData{ //Job1 make retrofit call to server //Job2 make retrofit…
julioribeiro
  • 973
  • 1
  • 10
  • 20
14
votes
1 answer

Using coroutines in retrofit authenticator

I'm trying to implement JWT token authentication and refresh token in retrofit using coroutines. The tokens are stored in a Room database. How should I implement the await call? Currently I'm using runBlocking {...} call to wait for the async…
Mihirrai
  • 145
  • 1
  • 4
14
votes
2 answers

Error calling Dispatchers.setMain() in unit test

Have started to try to use kotlinx-coroutines-test (https://github.com/Kotlin/kotlinx.coroutines/blob/master/core/kotlinx-coroutines-test/README.md) in JUnit unit test but getting following error when i call…
John O'Reilly
  • 8,536
  • 4
  • 31
  • 49
14
votes
2 answers

Whats the concept behind a CoroutineScope?

After reading the introduction and the javadoc of CoroutineScope I'm still a little confused what the idea behind a CoroutineScope is. The first sentence of the doc "Defines a scope for new coroutines." is not clear to me: Why do my coroutines need…
morpheus05
  • 4,461
  • 2
  • 26
  • 46
14
votes
1 answer

Kotlin Coroutine Unit Testing Error: Exception in thread "main @coroutine#1 @coroutine#2" java.lang.NullPointerException

I'm trying to unit test a presenter that I made with Kotlin coroutines, and this is also my first time using Mockito Whenever I try to run the unit test I'm getting the following error the first time it tries to do anything with my view while in the…
Ben987654
  • 2,534
  • 2
  • 21
  • 41
13
votes
1 answer

How to call Kotlin suspending coroutine function from Java 7

I'm trying to call Kotlin function from Java 7. I'm using coroutines and this called function is suspending, for example: suspend fun suspendingFunction(): Boolean { return async { longRunningFunction() }.await() } suspend fun…
michalbrz
  • 2,702
  • 1
  • 22
  • 32
1
2 3
19 20