electrolobzik
02/10/2024, 1:42 PMsuspendCoroutine
I can’t use suspend functions inside to call collect
. What is the most correct way to do that?
private suspend fun uploadMedia(
repository: Repository
): Result = suspendCoroutine { continuation ->
repository.uploadMedia().collect { // <-- Can't call collect
when (it) {
...
}
}
}
hfhbd
02/10/2024, 1:59 PMsuspendCororoutine
at all? It’s a wrapper for callback code. Just remove it, your function is already suspend
.streetsofboston
02/10/2024, 6:21 PMFlow.first()
will do this for youstreetsofboston
02/10/2024, 6:22 PMFlow
in your example is repository.uploadMedia()
electrolobzik
02/10/2024, 9:19 PMuploadMedia
method and return Result.electrolobzik
02/10/2024, 9:30 PM'return' is not allowed here
electrolobzik
02/10/2024, 9:37 PMelectrolobzik
02/10/2024, 10:13 PMlouiscad
02/11/2024, 11:18 AMsuspendCoroutine
at all because it can prevent cancellation, leading to memory leaks (leaking other unrelated objects) or suspend locks.
You want to use suspendCancellableCoroutine
instead, and handle cancellation correctly when the single-callback based API you're wrapping supports it.louiscad
02/11/2024, 11:19 AMrepository.uploadMedia().collect { … }
here, without wrapping it.electrolobzik
02/11/2024, 3:11 PM