what's the differences between sendOrNothing vs trySend of Coroutine Channel?

152 Views Asked by At

I wanted to know how to pass lifeCycleScope or viewModelScope to WebSocketListener class.

And I couldn't find the solution and ended up to using GlobalScope like below:

override fun onFailure(webSocket: WebSocket, t: Throwable, response: Response?) {
    GlobalScope.launch {
        socketEventChannel.sendOrNothing(SocketUpdate(exception = t))
    }
}

And I've found there's trySend for channel like below.

override fun onFailure(webSocket: WebSocket, t: Throwable, response: Response?) {
    socketEventChannel.trySend(SocketUpdate(exception = t))
}

What's the difference? Do they have any pros and cons? And which way is better?

0

There are 0 best solutions below