Home > OS >  Is it necessary to call .isSuccess?
Is it necessary to call .isSuccess?

Time:10-14

I need to read the content of a collection in real-time. Here is what I have tried:

override fun getItems() = callbackFlow {
    val listener = db.collection("items").addSnapshotListener { snapshot, e ->
        val response = if (snapshot != null) {
            val items = snapshot.toObjects(Item::class.java)
            Response.Success(items)
        } else {
            Response.Error(e)
        }
        trySend(response).isSuccess //???
    }
    awaitClose {
        listener.remove()
    }
}

And it works fine. The problem is that I don't understand the purpose of .isSuccess. Is it mandatory to be added?

CodePudding user response:

trySend() returns a ChannelResult object which contains the result of the operation. If ChannelResult.isSuccess returns true then the response had been successfully sent, otherwise the operation has been failed for some reason (maybe because of the buffer overflow) or because of a coroutine had been finished. You may handle it if you want, but usually it's omitted. Or you may log this result.

  • Related