Aslam Hossin
03/02/2021, 8:48 AMsuspend fun <T> wrapStatusListener(value: T, block: (StatusListener) -> Unit): T {
return suspendCancellableCoroutine { continuation: CancellableContinuation<T> ->
block(object : StatusListener() {
override fun onSuccess() {
continuation.resume(value)
}
override fun onError(errorInfo: ErrorInfo) {
continuation.resumeWithException(
Exception(
code = errorInfo.code,
status = errorInfo.status,
errorMessage = errorInfo.message
)
)
}
})
}
}
uli
03/02/2021, 9:04 AMonSuccess
is called twice by block
onSuccess
being called after onError
Aslam Hossin
03/02/2021, 9:23 AMuli
03/02/2021, 11:22 AMAslam Hossin
03/02/2021, 11:44 AMkevin.cianfarini
03/02/2021, 3:03 PMStatusListener
is a callback which gets invoked more than once. If that’s the case, you’re looking for a Flow
, not suspend coroutineAslam Hossin
03/02/2021, 4:00 PMZach Klippenstein (he/him) [MOD]
03/02/2021, 4:38 PMawaitStatus()
, I would expect it to just wait for the next status update and return it. In that case you could use an AtomicBoolean to ensure that you only invoke resume once.
However, if you actually want to be able to return multiple status updates, then your function should return a Flow and probably use the callbackFlow
builder, as previously suggested.uli
03/02/2021, 5:09 PMblock
return? Is there a way to terminate?
I mean you are using suspendCancellableCoroutine
and not handling cancelation. Maybe this is even the problem.
Try to either find a way how to ‘unregister’ your status listener on cancelation or use suspendCoroutine
.
If your can unregister and your issue persists, you should also unregister in onSuccess
and onError
to avoid further calls to the listener.suspendCoroutine
and please let us know if this solved your issue.Aslam Hossin
03/03/2021, 10:14 AM