Search code examples
iosswiftfirebasegoogle-cloud-firestorelistener

Is there a way to listen for changes in a collection WITHOUT getting back the documents in Firestore


Suppose I have a collection called 'books' and a page called "all books" in my app. Now let's say I want a little message to pop up in the top right that says "This list is outdated" every time there is an addition, deletion, or change in my 'books' collection. Is there any way to achieve this without having the listener send back all the documents in the 'books' collection each time there is a change? I only want to be notified when the data is outdated and nothing else.

If you know how to achieve this please let me know. I'm good with any language but a solution written in Swift will be preferred.

Thanks in advance!


Solution

  • Just add a field updatedon to your books document and set it to the current time when doing any write (on delete also then, you will have to find a way to hide deleted books).

    And set a listener like this:

    colref.where("updatedon", ">", new Date())
          .orderBy("updatedon","desc")
          .limit(1)
    

    This way you are only billed 1 read every time.