https://kotlinlang.org logo
#coroutines
Title
# coroutines
s

Santiago Laport

07/30/2020, 9:53 PM
Hello guys, first of all my apologies for my bad English :) I have an exception in my code, probably I need to do it in a better way but I dont know how yet. I want to call a bunch of listeners of a library as a sequence, so I added a continuation in every callback The code is more less like this:
Copy code
fun method(){
    viewModelScope.launch {
        val result1 = getSomeData1()
        val result2 = getSomeData2()
        val result3=  getSomeData3()
        ...
    }
}

suspend fun getSomeData1() : String? = suspendCoroutine { continuation ->
    someInstance.addListener(SomeListener {
                continuation.resume(data)
            })
}
...
First time works well, but sometimes I need to execute again and I get the next exception:
IllegalStateException : Already resumed
this occurs in in the
continuation.resume
o

octylFractal

07/30/2020, 9:56 PM
can the listener be called more than once? if so, you need to prevent that
most likely you want to detach the listener before/after you resume the coroutine
1
😀 1
k

kevindmoore

07/30/2020, 10:12 PM
Every time you call getSomeData1, you are adding a new anonymous listener.
😀 1
s

Santiago Laport

07/30/2020, 10:26 PM
You are right guys, the problem was that the listener is called more than once for some reason, but removing the listener(as Octavia says) after the resume works well
2 Views