Sebastien Leclerc Lavallee
08/31/2020, 2:55 PMAuthState.performActionWithFreshTokens(callback: (freshToken: String?))
which uses a callback to give me the fresh token. And my transport service uses coroutines, something like:
class TransportService {
suspend fun getAllTasks(): List<Task> {
authState.performActionWithFreshToken() { freshToken ->
return ktorClient.get<List<Task>>()
}
}
}
Now, I have an error where I use the ktor client: suspend function can be called only within coroutines scope
. That is because the callback function is not a coroutine scope. If I create a new scope within the callback, will this work well with the main function? What can I do to keep my transport with suspend function and not use completion handler everywhere? Thanks 🙂streetsofboston
08/31/2020, 2:59 PMsuspend fun getAllTasks(): List<Task> = suspendCancellableCoroutine { cont ->
authState.performActionWithFreshToken() { freshToken ->
cont.resume(ktorClient.get<List<Task>>())
}
}
suspendCancellableCoroutine
)Sebastien Leclerc Lavallee
08/31/2020, 3:59 PMcont
which take a closure to execute more suspend function, something like :
suspend fun getAllTasks(): List<Task> = suspendCancellableCoroutine { cont ->
authState.performActionWithFreshToken() { freshToken ->
cont.resume() {
var list = listOf<Task>()
do {
list.addAll(ktorClient.get<List<Task>>())
}
while(moreTask)
return list
}
}
}
It’s possible with suspendCancellableCoroutine
? Thanksstreetsofboston
08/31/2020, 4:06 PMsuspend fun getAllTasks(): List<Task> = suspendCancellableCoroutine { cont ->
authState.performActionWithFreshToken() { freshToken ->
var list = listOf<Task>()
do {
list.addAll(ktorClient.get<List<Task>>())
}
while(moreTask)
cont.resume(list)
}
}
Sebastien Leclerc Lavallee
08/31/2020, 4:08 PMktorClient.get<List<Task>>()
is a suspend function and when called it’s not in the coroutine scope anymore because of the callback 🤔 I have the same errorcont.resume()
when I’m finished