0

I am writing a Kotlin app and am using Firestore as my database. I am holding the document of the currently logged-in user locally as I use it constantly. I listen to changes like so:

    private val document = MutableLiveData<DocumentSnapshot?>()
    private var registration: ListenerRegistration? = null

    protected open fun switchDocument(id: String): Task<DocumentSnapshot>? {

        var task: Task<DocumentSnapshot>? = null

        // If no id was given - clear document
        if (id.isEmpty()) {
            registration?.remove()
            document.value = null

            // If the new id belongs to another document then the one we currently refer to
        } else if ((model.value?.id ?: "") != id) {

            // If a change listener is registered to the previous document - remove it
            registration?.remove()

            // Get the current document reference
            val newDocRef = Database.collection(collectionPath).document(id)

            // Save the document locally
            task = newDocRef.get()
            task.addOnSuccessListener { document.value = it }
                .addOnFailureListener { exception ->
                    Log.d(TAG, "get failed with ", exception)
                }

            // Listen for changes in the document
            registration = newDocRef.addSnapshotListener { snapshot, e ->
                if (e != null) Log.w(TAG, "Listen failed.", e)
                else document.value = snapshot
            }
        }

        return task
    }


This works perfectly for all flat fields in my document. I now have a new field which is a map value. When I set a new value in the map (with docRef?.update("${map-value-field-name}.${map-key}", map-value)) the listener is not called and I don't get the updated data unless I call docRef?.get()?.addOnSuccessListener { document.value = it }. Is there a way to listen to a map-value inside a Firestore document?

EDIT: In order to clarify, I am not looking for a way to know the particular data that changed in the document - I know that Firestore's "atom" is the document and I'm good with that. My problem is that the listener doesn't fire at all for some changes in the document (values inside a map-value).

Omer Levy
  • 347
  • 4
  • 11
  • You cannot listen to only a field in the document. All operations in Firestore are on an entire document. – Alex Mamo Aug 16 '21 at 14:24
  • @AlexMamo I know, but `addSnapshotListener` is triggered whenever a field is updated. Why doesn't it get triggered when a deep field is changed? the update is on the hole document - I don't care what field changed, I want the document to be reloaded. – Omer Levy Aug 16 '21 at 15:41
  • As long as the value of a field is changed, the listener fires, no matter what is the type of the field. – Alex Mamo Aug 16 '21 at 15:52
  • @AlexMamo This is what I thought, but when I change a value inside a map-value it doesn't fire – Omer Levy Aug 17 '21 at 08:36

0 Answers0