database-ktx: add callbackFlow for eventlisteners#4012
Conversation
Coverage Report 1Affected Products
Test Logs
|
Size Report 1Affected Products
Test Logs
|
| fun Query.snapshots() = callbackFlow<DataSnapshot> { | ||
| val listener = addValueEventListener(object : ValueEventListener { | ||
| override fun onDataChange(snapshot: DataSnapshot) { | ||
| trySend(snapshot) |
There was a problem hiding this comment.
For 7235473 we went with trySendBlocking is there any difference between cases?
There was a problem hiding this comment.
- I think it is a false positive in the linter.
- That said, it looks like the events are dispatched on the UI thread, so it's not ok to directly
trySendBlocking:
imo instead we can do the same thing we did for firestore, something like
| trySend(snapshot) | |
| someExecutor.execute(() -> trySendBlocking(snapshot)); |
There was a problem hiding this comment.
@vkryachko Looks like firestore's background executor uses a ThreadPool with 4 threads:
Should we also use a ThreadPool for RTDB?
| trySend(snapshot) | |
| Executors.newFixedThreadPool(nThreads = 4).execute { | |
| trySendBlocking(ChildEvent.Added(snapshot, previousChildName)) | |
| } |
There was a problem hiding this comment.
@vkryachko You were right, it was a false-positive in the linter. After the AGP7 update, the lint warning went away.
But my question on what executor we'll want to use remains.
There was a problem hiding this comment.
Not sure if that is safe, i.e. isn't in happening on the UI thread?
@maneesht what are your thoughts?
There was a problem hiding this comment.
It seems like scheduleNow() delegates to DefaultRunLoop which uses a ScheduledThreadPoolExecutor , so it doesn't block the UI Thread.
There was a problem hiding this comment.
My only concern with this is that we would end up blocking other operations within the Repo's actions. Maybe we should create a new executor solely for this? As RTDB only has one ATM
There was a problem hiding this comment.
I thought this through a bit more, and let's go ahead and just use the default run loop. If we feel like there's a performance gain, we can always change this in a future patch.
firebase-database/ktx/src/main/kotlin/com/google/firebase/database/ktx/Database.kt
Outdated
Show resolved
Hide resolved
|
@vkryachko I changed the Flows to be properties instead of functions. Please take a second look |

Part of go/kotlin-flows-firebase-android
This PR should add callbackFlows for RTDB's
ValueEventListenerandChildEventListener.