Code at collect{} from callbackFlow is called only one time even after change in database

Viewed 296

Every time when some data is changed in Users collection I want to get notify and do another callback to Groups collection and then emit some data like in the code below.

When I go to activity who listen to getUserGroups() function I get this groups. So that's great. But then when some data is added to Users only the getUser() callback is Logging me user - the collect{} is not collecting this value which is sent by this.trysend() anymore. Why?

suspend fun getUserGroups(): Flow<List<Group>?> = flow {
    getUser().collect { user ->
        Timber.d("user collected")
        getGroups(user).collect { groups ->
            Timber.d("groups collected")
            emit(groups)
        }
    }
}

private suspend fun getUser(): Flow<User> = callbackFlow {
    var user: User
    val snapshotListener = db.collection("Users")
        .whereEqualTo("email", auth.currentUser?.email)
        .addSnapshotListener { snapshot, _ ->
            if (snapshot != null) {
                for (document in snapshot) {
                    user = document.toObject(User::class.java)
                    Timber.d(user.groups.toString())
                    this.trySend(user)
                }
            }
        }
    awaitClose {
        snapshotListener.remove() }
}

private suspend fun getGroups(user: User): Flow<List<Group>> = callbackFlow {
    if (!user.groups.isNullOrEmpty()) {
        val collection = db.collection("Groups")
            .whereIn(FieldPath.documentId(), user.groups)

        val snapshotListener = collection.addSnapshotListener { snapshot, error ->
            if (error == null) {
                Timber.d("groups snapshot: ${snapshot?.documents.toString()}")
                this.trySend(snapshot!!.toObjects(Group::class.java)).isSuccess
            } else {
                cancel(message = "Error while getting users chats.", cause = error)
            }
        }
        awaitClose { snapshotListener.remove() }
    } else {
        trySend(emptyList())
        awaitClose { cancel() }
    }
}

The process is shown in Logs below. The last log is shown after adding manualny new group to Firestore DB:

2021-12-29 18:37:18.340 1000-1000/com.wk.locals D/GroupsDataSource$getUser: [com.google.firebase.firestore.DocumentReference@7cb26ec7]
2021-12-29 18:37:18.342 1000-1000/com.wk.locals D/GroupsDataSource$getUserGroups: user collected
2021-12-29 18:37:18.384 1000-1000/com.wk.locals D/GroupsDataSource$getGroups: groups snapshot: [DocumentSnapshot{key=Groups/ZaKxPfkRuAAv2P3o2nuu, metadata=SnapshotMetadata{hasPendingWrites=false, isFromCache=true}, doc=Document{key=Groups/ZaKxPfkRuAAv2P3o2nuu, version=SnapshotVersion(seconds=1640781210, nanos=559592000), type=FOUND_DOCUMENT, documentState=SYNCED, value=ObjectValue{internalValue={avatar:null,hobbies:[muzyka,rozrywka,chór,ludowe,politechnika opolska],location:{geohash:u3hb266,latitude:50.681605,longitude:17.943905},members:[{avatar:null,name:Tadeusz,surname:Katoda,userId:D6zh5u4XvZqCKvCAvkiF},{avatar:null,name:Irena,surname:Kula,userId:pLJb8tyuQcvbbZovGFjn},{avatar:null,name:Karol,surname:Kowalski,userId:Ab8TI3ff2RUHihOGRfS6},{avatar:null,name:Wojciech,surname:Kula,userId:XP0Pr8gTEWT7qJjfI3SX}],name:Chór ACPOk}}}}]
2021-12-29 18:37:18.400 1000-1000/com.wk.locals D/GroupsDataSource$getUserGroups: groups collected
2021-12-29 18:37:36.969 1000-1000/com.wk.locals D/GroupsDataSource$getUser: [com.google.firebase.firestore.DocumentReference@7cb26ec7, com.google.firebase.firestore.DocumentReference@b49bac12]
0 Answers
Related