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).