0
votes

I'm trying to get the addSnapshotListener to fire when a referenced document inside an other document changes.

I have two tables: users and rooms.

There is a reference to a user inside rooms

rooms > {roomId} > room_users (which is a collection) > [list of id's] > reference to a user in each document

enter image description here

Using the following code I get a reference to this

fun getInnerCollection(baseCollection: String, baseDoc: String, collection: String): CollectionReference {
     return FirebaseUtil.firestore
          .collection(baseCollection)
          .document(baseDoc)
          .collection(collection)
     }

On this I add an addSnapshotListener.

Any time I change (ar add) a field in any of the documents inside room_users, the listener fires. However when I changed a property in the users table, that is linked in a document (like OMF1JtKgj3PZaEsUkitj900FIYm1 in the screenshot), the listener doesn't fire. Because of this I'm not aware when something changes in a referenced document.

How would I make the listener fire when a change occurs inside a referenced object? Or is adding a listener to every document the only solution?

1
I supposed that it cannot be implemented. The Firebase snapshot listener does not can listen to what a reference field points to.Vinicius Almada

1 Answers

1
votes

A query can only consider documents in a single collection. Therefore, a listener on that query can only respond to changes to documents in that single collection. It will not consider any references to other documents.

If you want to listen to a document that comes from a DocumentReference field, you will need to establish a new listener for each one separately.