Is there a method to fully drain a `Channel`?
# coroutines
r
Is there a method to fully drain a
Channel
?
k
What do you mean by drain?
r
Empty it of all elements. I want to reset it.
j
While true, try receive, break on no element
☝️ 1
r
Copy code
while (requests.tryReceive().isSuccess) {
            Log.d(TAG, "Dropped a request")
        }
Now can I access the request dropped? 😄
k
Copy code
private fun <T> RecieveChannel<T>.drain() {
  var hasElements = true
  while (hasElements) {
    hasElements = tryRecieve().isSuccess
  }
}
yes you have to unwrap the result with something like
getOrThrow
or
getOrNull
or
getOrElse
.
j
Could use toList as well. Which consume all current elements and return into a list. Not sure if wanted in this case
j
That would block until the channel is closed. Also cause a bunch of allocations
r
Decided to just throw everything away instead of resetting, certainly cleaner ^^