Colton Idle
05/11/2022, 1:23 AMoverride suspend fun requestBooks(): Flow<List<Book>> {
return callbackFlow {
val listener =
FirebaseFirestore.getInstance().collection("books").addSnapshotListener { value, error
->
trySend(value!!.toObjects())
}
awaitClose { listener.remove() }
}
}
How do I move the trySend(value!!.toObjects())
to a background thread. My issue is that value!!.toObjects()
is a deserialization operation that can take a long time.
Wrapping trySend
in a launch with Dispatchers.IO seems to help my lag issue in my app, but is it really that easy? Or am i shooting myself in the foot.
launch(<http://Dispatchers.IO|Dispatchers.IO>) {
trySend(value!!.toObjects())
}
ephemient
05/11/2022, 1:40 AMColton Idle
05/11/2022, 1:42 AMephemient
05/11/2022, 1:43 AMblockingSend
to a channel
, and then collected from the channel.collectAsFlow().buffer()
, is effectively queuedtrySend().onFailure { throw }
toObjects
to another thread though…callbackFlow {
addListener { value, error ->
trySend(value!!).onFailure { throw it }
}
}.map { value.toObjects() }.flowOn(<http://Dispatchers.IO|Dispatchers.IO>).buffer()
would do thatColton Idle
05/11/2022, 1:58 AMephemient
05/11/2022, 11:01 AM