How to skip initial data and trigger only new updates in Firestore Firebase?










3















I've searched everywhere with no luck. I want to query Firestore to get all users WHERE type is admin. Something like:



SELECT * FROM users WHERE type=admin


but only when the property total is changing. If I'm using:



users.whereEqualTo("type", "admin").addSnapshotListener(new EventListener<QuerySnapshot>() 
@Override
public void onEvent(@Nullable QuerySnapshot snapshots, @Nullable FirebaseFirestoreException e)
for (DocumentChange dc : snapshots.getDocumentChanges())
switch (dc.getType())
case ADDED:
//Not trigger
break;
case MODIFIED:
//Trigger
break;
case REMOVED:
//
break;



);


The case ADDED is triggered first time when I query and when the total is changed case MODIFIED is triggered again (this is what is want). I want only changes and not the all initial data, I don't need it. How to get it?



Please help me, is the last part of my project. How to skip is case ADDED?










share|improve this question




























    3















    I've searched everywhere with no luck. I want to query Firestore to get all users WHERE type is admin. Something like:



    SELECT * FROM users WHERE type=admin


    but only when the property total is changing. If I'm using:



    users.whereEqualTo("type", "admin").addSnapshotListener(new EventListener<QuerySnapshot>() 
    @Override
    public void onEvent(@Nullable QuerySnapshot snapshots, @Nullable FirebaseFirestoreException e)
    for (DocumentChange dc : snapshots.getDocumentChanges())
    switch (dc.getType())
    case ADDED:
    //Not trigger
    break;
    case MODIFIED:
    //Trigger
    break;
    case REMOVED:
    //
    break;



    );


    The case ADDED is triggered first time when I query and when the total is changed case MODIFIED is triggered again (this is what is want). I want only changes and not the all initial data, I don't need it. How to get it?



    Please help me, is the last part of my project. How to skip is case ADDED?










    share|improve this question


























      3












      3








      3








      I've searched everywhere with no luck. I want to query Firestore to get all users WHERE type is admin. Something like:



      SELECT * FROM users WHERE type=admin


      but only when the property total is changing. If I'm using:



      users.whereEqualTo("type", "admin").addSnapshotListener(new EventListener<QuerySnapshot>() 
      @Override
      public void onEvent(@Nullable QuerySnapshot snapshots, @Nullable FirebaseFirestoreException e)
      for (DocumentChange dc : snapshots.getDocumentChanges())
      switch (dc.getType())
      case ADDED:
      //Not trigger
      break;
      case MODIFIED:
      //Trigger
      break;
      case REMOVED:
      //
      break;



      );


      The case ADDED is triggered first time when I query and when the total is changed case MODIFIED is triggered again (this is what is want). I want only changes and not the all initial data, I don't need it. How to get it?



      Please help me, is the last part of my project. How to skip is case ADDED?










      share|improve this question
















      I've searched everywhere with no luck. I want to query Firestore to get all users WHERE type is admin. Something like:



      SELECT * FROM users WHERE type=admin


      but only when the property total is changing. If I'm using:



      users.whereEqualTo("type", "admin").addSnapshotListener(new EventListener<QuerySnapshot>() 
      @Override
      public void onEvent(@Nullable QuerySnapshot snapshots, @Nullable FirebaseFirestoreException e)
      for (DocumentChange dc : snapshots.getDocumentChanges())
      switch (dc.getType())
      case ADDED:
      //Not trigger
      break;
      case MODIFIED:
      //Trigger
      break;
      case REMOVED:
      //
      break;



      );


      The case ADDED is triggered first time when I query and when the total is changed case MODIFIED is triggered again (this is what is want). I want only changes and not the all initial data, I don't need it. How to get it?



      Please help me, is the last part of my project. How to skip is case ADDED?







      java android firebase google-cloud-firestore






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 5 '18 at 14:30









      Alex Mamo

      42.8k82860




      42.8k82860










      asked Nov 5 '18 at 14:13









      Sky TrackerSky Tracker

      184




      184






















          2 Answers
          2






          active

          oldest

          votes


















          4














          When you are listening for changes in Cloud Firestore for realtime changes, using Firestore Query's addSnapshotListener() method, it:




          Starts listening to this query.




          Which basically means that first time you attach the listener, you get all documents that correspond to that particular query. Furthermore, everytime a property within a document changes, you are notified according to that change. Obviously, this is happening only if the listener remains active and is not removed.



          Unfortunately, Firestore listeners don't work that way, so you cannot skip that "case ADDED". What you can do instead, is to add add under each user object a Date property (this is how you can add it) and query your database on client, according to this new property, for all documents that have changed since a previous time.



          According to Nick Cardoso's comment, for future visitors that might ask why this behaviour happens, is because the reason he mentioned in his comment. I also recommend see Doug Stevenson's answer from this post, for a better understanding.






          share|improve this answer

























          • Thank for the answer, I try your solution.

            – Sky Tracker
            Nov 5 '18 at 17:49











          • Ok, keep me posted.

            – Alex Mamo
            Nov 5 '18 at 18:04











          • Hi Sky! Have you tried my solution above, does it work?

            – Alex Mamo
            Nov 6 '18 at 7:44











          • This answer is incomplete as it misses a big point (the date solves the issue, but doesn't explain why the behaviour happens). The snapshot provides data from the local cache as well as mutations from the remote. So if you only wan't changes (implying this is a repeated query) it's not fetching (and charging for) everything again. It's getting just the mutations as expected, you just have to handle it appropriately.

            – Nick Cardoso
            Nov 9 '18 at 9:25







          • 1





            @NickCardoso Regarding your question, hope that the down-voter will also provide a reason, as you did in my case. Just up-voted because I found your question very helpful and hope you'll find an answer soon.

            – Alex Mamo
            Nov 9 '18 at 9:48



















          0














          Here is a solution working for me:
          use



          AtomicBoolean isFirstListener = new AtomicBoolean(true);


          and then on event method



          if (isFirstListener.get()) 
          isFirstListener.set(false);
          //TODO Handle the entire list.
          return;



          Here is a sample code from my project:



           final AtomicBoolean isFirstListener = new AtomicBoolean(true);
          mDb.collection("conversation_log").document(room_id).collection("messages").orderBy("sent_at")
          .addSnapshotListener(new EventListener<QuerySnapshot>()
          @Override
          public void onEvent(@Nullable QuerySnapshot value2, @Nullable FirebaseFirestoreException e)
          if (isFirstListener.get())
          isFirstListener.set(false);
          //TODO Handle the entire list.
          return;


          );


          reference: answer






          share|improve this answer






















            Your Answer






            StackExchange.ifUsing("editor", function ()
            StackExchange.using("externalEditor", function ()
            StackExchange.using("snippets", function ()
            StackExchange.snippets.init();
            );
            );
            , "code-snippets");

            StackExchange.ready(function()
            var channelOptions =
            tags: "".split(" "),
            id: "1"
            ;
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function()
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled)
            StackExchange.using("snippets", function()
            createEditor();
            );

            else
            createEditor();

            );

            function createEditor()
            StackExchange.prepareEditor(
            heartbeatType: 'answer',
            autoActivateHeartbeat: false,
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader:
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            ,
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            );



            );













            draft saved

            draft discarded


















            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53156109%2fhow-to-skip-initial-data-and-trigger-only-new-updates-in-firestore-firebase%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown

























            2 Answers
            2






            active

            oldest

            votes








            2 Answers
            2






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            4














            When you are listening for changes in Cloud Firestore for realtime changes, using Firestore Query's addSnapshotListener() method, it:




            Starts listening to this query.




            Which basically means that first time you attach the listener, you get all documents that correspond to that particular query. Furthermore, everytime a property within a document changes, you are notified according to that change. Obviously, this is happening only if the listener remains active and is not removed.



            Unfortunately, Firestore listeners don't work that way, so you cannot skip that "case ADDED". What you can do instead, is to add add under each user object a Date property (this is how you can add it) and query your database on client, according to this new property, for all documents that have changed since a previous time.



            According to Nick Cardoso's comment, for future visitors that might ask why this behaviour happens, is because the reason he mentioned in his comment. I also recommend see Doug Stevenson's answer from this post, for a better understanding.






            share|improve this answer

























            • Thank for the answer, I try your solution.

              – Sky Tracker
              Nov 5 '18 at 17:49











            • Ok, keep me posted.

              – Alex Mamo
              Nov 5 '18 at 18:04











            • Hi Sky! Have you tried my solution above, does it work?

              – Alex Mamo
              Nov 6 '18 at 7:44











            • This answer is incomplete as it misses a big point (the date solves the issue, but doesn't explain why the behaviour happens). The snapshot provides data from the local cache as well as mutations from the remote. So if you only wan't changes (implying this is a repeated query) it's not fetching (and charging for) everything again. It's getting just the mutations as expected, you just have to handle it appropriately.

              – Nick Cardoso
              Nov 9 '18 at 9:25







            • 1





              @NickCardoso Regarding your question, hope that the down-voter will also provide a reason, as you did in my case. Just up-voted because I found your question very helpful and hope you'll find an answer soon.

              – Alex Mamo
              Nov 9 '18 at 9:48
















            4














            When you are listening for changes in Cloud Firestore for realtime changes, using Firestore Query's addSnapshotListener() method, it:




            Starts listening to this query.




            Which basically means that first time you attach the listener, you get all documents that correspond to that particular query. Furthermore, everytime a property within a document changes, you are notified according to that change. Obviously, this is happening only if the listener remains active and is not removed.



            Unfortunately, Firestore listeners don't work that way, so you cannot skip that "case ADDED". What you can do instead, is to add add under each user object a Date property (this is how you can add it) and query your database on client, according to this new property, for all documents that have changed since a previous time.



            According to Nick Cardoso's comment, for future visitors that might ask why this behaviour happens, is because the reason he mentioned in his comment. I also recommend see Doug Stevenson's answer from this post, for a better understanding.






            share|improve this answer

























            • Thank for the answer, I try your solution.

              – Sky Tracker
              Nov 5 '18 at 17:49











            • Ok, keep me posted.

              – Alex Mamo
              Nov 5 '18 at 18:04











            • Hi Sky! Have you tried my solution above, does it work?

              – Alex Mamo
              Nov 6 '18 at 7:44











            • This answer is incomplete as it misses a big point (the date solves the issue, but doesn't explain why the behaviour happens). The snapshot provides data from the local cache as well as mutations from the remote. So if you only wan't changes (implying this is a repeated query) it's not fetching (and charging for) everything again. It's getting just the mutations as expected, you just have to handle it appropriately.

              – Nick Cardoso
              Nov 9 '18 at 9:25







            • 1





              @NickCardoso Regarding your question, hope that the down-voter will also provide a reason, as you did in my case. Just up-voted because I found your question very helpful and hope you'll find an answer soon.

              – Alex Mamo
              Nov 9 '18 at 9:48














            4












            4








            4







            When you are listening for changes in Cloud Firestore for realtime changes, using Firestore Query's addSnapshotListener() method, it:




            Starts listening to this query.




            Which basically means that first time you attach the listener, you get all documents that correspond to that particular query. Furthermore, everytime a property within a document changes, you are notified according to that change. Obviously, this is happening only if the listener remains active and is not removed.



            Unfortunately, Firestore listeners don't work that way, so you cannot skip that "case ADDED". What you can do instead, is to add add under each user object a Date property (this is how you can add it) and query your database on client, according to this new property, for all documents that have changed since a previous time.



            According to Nick Cardoso's comment, for future visitors that might ask why this behaviour happens, is because the reason he mentioned in his comment. I also recommend see Doug Stevenson's answer from this post, for a better understanding.






            share|improve this answer















            When you are listening for changes in Cloud Firestore for realtime changes, using Firestore Query's addSnapshotListener() method, it:




            Starts listening to this query.




            Which basically means that first time you attach the listener, you get all documents that correspond to that particular query. Furthermore, everytime a property within a document changes, you are notified according to that change. Obviously, this is happening only if the listener remains active and is not removed.



            Unfortunately, Firestore listeners don't work that way, so you cannot skip that "case ADDED". What you can do instead, is to add add under each user object a Date property (this is how you can add it) and query your database on client, according to this new property, for all documents that have changed since a previous time.



            According to Nick Cardoso's comment, for future visitors that might ask why this behaviour happens, is because the reason he mentioned in his comment. I also recommend see Doug Stevenson's answer from this post, for a better understanding.







            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Nov 9 '18 at 9:46

























            answered Nov 5 '18 at 14:29









            Alex MamoAlex Mamo

            42.8k82860




            42.8k82860












            • Thank for the answer, I try your solution.

              – Sky Tracker
              Nov 5 '18 at 17:49











            • Ok, keep me posted.

              – Alex Mamo
              Nov 5 '18 at 18:04











            • Hi Sky! Have you tried my solution above, does it work?

              – Alex Mamo
              Nov 6 '18 at 7:44











            • This answer is incomplete as it misses a big point (the date solves the issue, but doesn't explain why the behaviour happens). The snapshot provides data from the local cache as well as mutations from the remote. So if you only wan't changes (implying this is a repeated query) it's not fetching (and charging for) everything again. It's getting just the mutations as expected, you just have to handle it appropriately.

              – Nick Cardoso
              Nov 9 '18 at 9:25







            • 1





              @NickCardoso Regarding your question, hope that the down-voter will also provide a reason, as you did in my case. Just up-voted because I found your question very helpful and hope you'll find an answer soon.

              – Alex Mamo
              Nov 9 '18 at 9:48


















            • Thank for the answer, I try your solution.

              – Sky Tracker
              Nov 5 '18 at 17:49











            • Ok, keep me posted.

              – Alex Mamo
              Nov 5 '18 at 18:04











            • Hi Sky! Have you tried my solution above, does it work?

              – Alex Mamo
              Nov 6 '18 at 7:44











            • This answer is incomplete as it misses a big point (the date solves the issue, but doesn't explain why the behaviour happens). The snapshot provides data from the local cache as well as mutations from the remote. So if you only wan't changes (implying this is a repeated query) it's not fetching (and charging for) everything again. It's getting just the mutations as expected, you just have to handle it appropriately.

              – Nick Cardoso
              Nov 9 '18 at 9:25







            • 1





              @NickCardoso Regarding your question, hope that the down-voter will also provide a reason, as you did in my case. Just up-voted because I found your question very helpful and hope you'll find an answer soon.

              – Alex Mamo
              Nov 9 '18 at 9:48

















            Thank for the answer, I try your solution.

            – Sky Tracker
            Nov 5 '18 at 17:49





            Thank for the answer, I try your solution.

            – Sky Tracker
            Nov 5 '18 at 17:49













            Ok, keep me posted.

            – Alex Mamo
            Nov 5 '18 at 18:04





            Ok, keep me posted.

            – Alex Mamo
            Nov 5 '18 at 18:04













            Hi Sky! Have you tried my solution above, does it work?

            – Alex Mamo
            Nov 6 '18 at 7:44





            Hi Sky! Have you tried my solution above, does it work?

            – Alex Mamo
            Nov 6 '18 at 7:44













            This answer is incomplete as it misses a big point (the date solves the issue, but doesn't explain why the behaviour happens). The snapshot provides data from the local cache as well as mutations from the remote. So if you only wan't changes (implying this is a repeated query) it's not fetching (and charging for) everything again. It's getting just the mutations as expected, you just have to handle it appropriately.

            – Nick Cardoso
            Nov 9 '18 at 9:25






            This answer is incomplete as it misses a big point (the date solves the issue, but doesn't explain why the behaviour happens). The snapshot provides data from the local cache as well as mutations from the remote. So if you only wan't changes (implying this is a repeated query) it's not fetching (and charging for) everything again. It's getting just the mutations as expected, you just have to handle it appropriately.

            – Nick Cardoso
            Nov 9 '18 at 9:25





            1




            1





            @NickCardoso Regarding your question, hope that the down-voter will also provide a reason, as you did in my case. Just up-voted because I found your question very helpful and hope you'll find an answer soon.

            – Alex Mamo
            Nov 9 '18 at 9:48






            @NickCardoso Regarding your question, hope that the down-voter will also provide a reason, as you did in my case. Just up-voted because I found your question very helpful and hope you'll find an answer soon.

            – Alex Mamo
            Nov 9 '18 at 9:48














            0














            Here is a solution working for me:
            use



            AtomicBoolean isFirstListener = new AtomicBoolean(true);


            and then on event method



            if (isFirstListener.get()) 
            isFirstListener.set(false);
            //TODO Handle the entire list.
            return;



            Here is a sample code from my project:



             final AtomicBoolean isFirstListener = new AtomicBoolean(true);
            mDb.collection("conversation_log").document(room_id).collection("messages").orderBy("sent_at")
            .addSnapshotListener(new EventListener<QuerySnapshot>()
            @Override
            public void onEvent(@Nullable QuerySnapshot value2, @Nullable FirebaseFirestoreException e)
            if (isFirstListener.get())
            isFirstListener.set(false);
            //TODO Handle the entire list.
            return;


            );


            reference: answer






            share|improve this answer



























              0














              Here is a solution working for me:
              use



              AtomicBoolean isFirstListener = new AtomicBoolean(true);


              and then on event method



              if (isFirstListener.get()) 
              isFirstListener.set(false);
              //TODO Handle the entire list.
              return;



              Here is a sample code from my project:



               final AtomicBoolean isFirstListener = new AtomicBoolean(true);
              mDb.collection("conversation_log").document(room_id).collection("messages").orderBy("sent_at")
              .addSnapshotListener(new EventListener<QuerySnapshot>()
              @Override
              public void onEvent(@Nullable QuerySnapshot value2, @Nullable FirebaseFirestoreException e)
              if (isFirstListener.get())
              isFirstListener.set(false);
              //TODO Handle the entire list.
              return;


              );


              reference: answer






              share|improve this answer

























                0












                0








                0







                Here is a solution working for me:
                use



                AtomicBoolean isFirstListener = new AtomicBoolean(true);


                and then on event method



                if (isFirstListener.get()) 
                isFirstListener.set(false);
                //TODO Handle the entire list.
                return;



                Here is a sample code from my project:



                 final AtomicBoolean isFirstListener = new AtomicBoolean(true);
                mDb.collection("conversation_log").document(room_id).collection("messages").orderBy("sent_at")
                .addSnapshotListener(new EventListener<QuerySnapshot>()
                @Override
                public void onEvent(@Nullable QuerySnapshot value2, @Nullable FirebaseFirestoreException e)
                if (isFirstListener.get())
                isFirstListener.set(false);
                //TODO Handle the entire list.
                return;


                );


                reference: answer






                share|improve this answer













                Here is a solution working for me:
                use



                AtomicBoolean isFirstListener = new AtomicBoolean(true);


                and then on event method



                if (isFirstListener.get()) 
                isFirstListener.set(false);
                //TODO Handle the entire list.
                return;



                Here is a sample code from my project:



                 final AtomicBoolean isFirstListener = new AtomicBoolean(true);
                mDb.collection("conversation_log").document(room_id).collection("messages").orderBy("sent_at")
                .addSnapshotListener(new EventListener<QuerySnapshot>()
                @Override
                public void onEvent(@Nullable QuerySnapshot value2, @Nullable FirebaseFirestoreException e)
                if (isFirstListener.get())
                isFirstListener.set(false);
                //TODO Handle the entire list.
                return;


                );


                reference: answer







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Jan 7 at 13:03









                RajRaj

                13311




                13311



























                    draft saved

                    draft discarded
















































                    Thanks for contributing an answer to Stack Overflow!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid


                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.

                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function ()
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53156109%2fhow-to-skip-initial-data-and-trigger-only-new-updates-in-firestore-firebase%23new-answer', 'question_page');

                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    Popular posts from this blog

                    𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

                    Edmonton

                    Crossroads (UK TV series)