Firebase Firestore database listener to GlobalScope.rxFlowable


#1

How to wrap Firebase Firestore database listeners to a GlobalScope.rxFlowable. Given that Firestore callback called every time when the database updated.

 private fun <T> Query.realTimeUpdates(klass: Class<T>): Flowable<FirestoreResult> = GlobalScope.rxFlowable {
        suspendCancellableCoroutine {
            addSnapshotListener { collectionSnapshot: QuerySnapshot?, _: FirebaseFirestoreException? ->
                launch {
                    logD(TAG, "realTimeUpdates query -> in thread ${Thread.currentThread().name}")
                    if (collectionSnapshot != null && !collectionSnapshot.isEmpty)
                        send(FirestoreResult.Success(collectionSnapshot.toObjects(klass)))
                    else send(FirestoreResult.Error("Error : unknown error."))
                }
            }
        }
    }

Thanks