Should I actually remove the valueEventListener?










0















 DatabaseReference Ref = FirebaseDatabase.getInstance().getReference(Constants.Client + "/" + path);
Ref.keepSynced(true);
Ref.addValueEventListener(new ValueEventListener()
@Override
public void onDataChange(DataSnapshot dataSnapshot)



@Override
public void onCancelled(DatabaseError databaseError)


);


I understand that valueEventListener runs in a new thread, should I actually remove this at any point of time for proper thread management?(example for not too many threads running in parallel), If yes, how to do it?










share|improve this question
























  • Yeah . You should remove it in your components lifecycle . Othewise it will provide you callback without knowing the state of the Component(refer as Activity) . Remove the listener in onStop() or onDestroy().

    – ADM
    Feb 19 '18 at 7:34
















0















 DatabaseReference Ref = FirebaseDatabase.getInstance().getReference(Constants.Client + "/" + path);
Ref.keepSynced(true);
Ref.addValueEventListener(new ValueEventListener()
@Override
public void onDataChange(DataSnapshot dataSnapshot)



@Override
public void onCancelled(DatabaseError databaseError)


);


I understand that valueEventListener runs in a new thread, should I actually remove this at any point of time for proper thread management?(example for not too many threads running in parallel), If yes, how to do it?










share|improve this question
























  • Yeah . You should remove it in your components lifecycle . Othewise it will provide you callback without knowing the state of the Component(refer as Activity) . Remove the listener in onStop() or onDestroy().

    – ADM
    Feb 19 '18 at 7:34














0












0








0


1






 DatabaseReference Ref = FirebaseDatabase.getInstance().getReference(Constants.Client + "/" + path);
Ref.keepSynced(true);
Ref.addValueEventListener(new ValueEventListener()
@Override
public void onDataChange(DataSnapshot dataSnapshot)



@Override
public void onCancelled(DatabaseError databaseError)


);


I understand that valueEventListener runs in a new thread, should I actually remove this at any point of time for proper thread management?(example for not too many threads running in parallel), If yes, how to do it?










share|improve this question
















 DatabaseReference Ref = FirebaseDatabase.getInstance().getReference(Constants.Client + "/" + path);
Ref.keepSynced(true);
Ref.addValueEventListener(new ValueEventListener()
@Override
public void onDataChange(DataSnapshot dataSnapshot)



@Override
public void onCancelled(DatabaseError databaseError)


);


I understand that valueEventListener runs in a new thread, should I actually remove this at any point of time for proper thread management?(example for not too many threads running in parallel), If yes, how to do it?







android multithreading firebase firebase-realtime-database






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Feb 19 '18 at 21:33









Peter Haddad

20.5k94157




20.5k94157










asked Feb 19 '18 at 7:31









Sreekanth KarumanaghatSreekanth Karumanaghat

2,01912956




2,01912956












  • Yeah . You should remove it in your components lifecycle . Othewise it will provide you callback without knowing the state of the Component(refer as Activity) . Remove the listener in onStop() or onDestroy().

    – ADM
    Feb 19 '18 at 7:34


















  • Yeah . You should remove it in your components lifecycle . Othewise it will provide you callback without knowing the state of the Component(refer as Activity) . Remove the listener in onStop() or onDestroy().

    – ADM
    Feb 19 '18 at 7:34

















Yeah . You should remove it in your components lifecycle . Othewise it will provide you callback without knowing the state of the Component(refer as Activity) . Remove the listener in onStop() or onDestroy().

– ADM
Feb 19 '18 at 7:34






Yeah . You should remove it in your components lifecycle . Othewise it will provide you callback without knowing the state of the Component(refer as Activity) . Remove the listener in onStop() or onDestroy().

– ADM
Feb 19 '18 at 7:34













2 Answers
2






active

oldest

votes


















5














When talking about listeners, yes, you need to remove them accordingly to the life-cycle of your activity and for this you need to use the following line of code:



databaseReference.removeEventListener(valueEventListener);


Remember if you don't do this, you'll end up wasting your battery and bandwidth. So:



  1. If you have added the listener in onStart you have to remove it in onStop.

  2. If you have added the listener in onResume you have to remove it in onPause.

  3. If you have added the listener in onCreate you have to remove it in onDestroy.

But remember onDestroy is not always called, so the last option in not always a good choice.



There is another approach in which there is no need to remove the listener and that is when using addListenerForSingleValueEvent:




Add a listener for a single change in the data at this location.







share|improve this answer

























  • Why is onDestroy not always called?

    – Sreekanth Karumanaghat
    Feb 19 '18 at 10:59






  • 1





    Please take a look here and here, for a better understanding.

    – Alex Mamo
    Feb 19 '18 at 11:05


















3














To remove the ValueEventListener, you can then do this:



Remove the anonymity of the listener.



Change the code from this:-



 Ref.addValueEventListener(new ValueEventListener() 
@Override
public void onDataChange(DataSnapshot dataSnapshot)



@Override
public void onCancelled(DatabaseError databaseError)


);


into this:



 ValueEventListener listener= new ValueEventListener() 
@Override
public void onDataChange(DataSnapshot dataSnapshot)



@Override
public void onCancelled(DatabaseError databaseError)


);
Ref.addValueEventListener(listener);



Now you will be able to remove the listener:



 @Override
public void onDestroy()
if (Ref != null && listener != null)
Ref.removeEventListener(listener);




You need to remove it, so the listener does not stay running in the other activity lifecycles like onDestroy()






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%2f48861350%2fshould-i-actually-remove-the-valueeventlistener%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









    5














    When talking about listeners, yes, you need to remove them accordingly to the life-cycle of your activity and for this you need to use the following line of code:



    databaseReference.removeEventListener(valueEventListener);


    Remember if you don't do this, you'll end up wasting your battery and bandwidth. So:



    1. If you have added the listener in onStart you have to remove it in onStop.

    2. If you have added the listener in onResume you have to remove it in onPause.

    3. If you have added the listener in onCreate you have to remove it in onDestroy.

    But remember onDestroy is not always called, so the last option in not always a good choice.



    There is another approach in which there is no need to remove the listener and that is when using addListenerForSingleValueEvent:




    Add a listener for a single change in the data at this location.







    share|improve this answer

























    • Why is onDestroy not always called?

      – Sreekanth Karumanaghat
      Feb 19 '18 at 10:59






    • 1





      Please take a look here and here, for a better understanding.

      – Alex Mamo
      Feb 19 '18 at 11:05















    5














    When talking about listeners, yes, you need to remove them accordingly to the life-cycle of your activity and for this you need to use the following line of code:



    databaseReference.removeEventListener(valueEventListener);


    Remember if you don't do this, you'll end up wasting your battery and bandwidth. So:



    1. If you have added the listener in onStart you have to remove it in onStop.

    2. If you have added the listener in onResume you have to remove it in onPause.

    3. If you have added the listener in onCreate you have to remove it in onDestroy.

    But remember onDestroy is not always called, so the last option in not always a good choice.



    There is another approach in which there is no need to remove the listener and that is when using addListenerForSingleValueEvent:




    Add a listener for a single change in the data at this location.







    share|improve this answer

























    • Why is onDestroy not always called?

      – Sreekanth Karumanaghat
      Feb 19 '18 at 10:59






    • 1





      Please take a look here and here, for a better understanding.

      – Alex Mamo
      Feb 19 '18 at 11:05













    5












    5








    5







    When talking about listeners, yes, you need to remove them accordingly to the life-cycle of your activity and for this you need to use the following line of code:



    databaseReference.removeEventListener(valueEventListener);


    Remember if you don't do this, you'll end up wasting your battery and bandwidth. So:



    1. If you have added the listener in onStart you have to remove it in onStop.

    2. If you have added the listener in onResume you have to remove it in onPause.

    3. If you have added the listener in onCreate you have to remove it in onDestroy.

    But remember onDestroy is not always called, so the last option in not always a good choice.



    There is another approach in which there is no need to remove the listener and that is when using addListenerForSingleValueEvent:




    Add a listener for a single change in the data at this location.







    share|improve this answer















    When talking about listeners, yes, you need to remove them accordingly to the life-cycle of your activity and for this you need to use the following line of code:



    databaseReference.removeEventListener(valueEventListener);


    Remember if you don't do this, you'll end up wasting your battery and bandwidth. So:



    1. If you have added the listener in onStart you have to remove it in onStop.

    2. If you have added the listener in onResume you have to remove it in onPause.

    3. If you have added the listener in onCreate you have to remove it in onDestroy.

    But remember onDestroy is not always called, so the last option in not always a good choice.



    There is another approach in which there is no need to remove the listener and that is when using addListenerForSingleValueEvent:




    Add a listener for a single change in the data at this location.








    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Aug 1 '18 at 16:31









    JGuo

    622522




    622522










    answered Feb 19 '18 at 9:22









    Alex MamoAlex Mamo

    41k72859




    41k72859












    • Why is onDestroy not always called?

      – Sreekanth Karumanaghat
      Feb 19 '18 at 10:59






    • 1





      Please take a look here and here, for a better understanding.

      – Alex Mamo
      Feb 19 '18 at 11:05

















    • Why is onDestroy not always called?

      – Sreekanth Karumanaghat
      Feb 19 '18 at 10:59






    • 1





      Please take a look here and here, for a better understanding.

      – Alex Mamo
      Feb 19 '18 at 11:05
















    Why is onDestroy not always called?

    – Sreekanth Karumanaghat
    Feb 19 '18 at 10:59





    Why is onDestroy not always called?

    – Sreekanth Karumanaghat
    Feb 19 '18 at 10:59




    1




    1





    Please take a look here and here, for a better understanding.

    – Alex Mamo
    Feb 19 '18 at 11:05





    Please take a look here and here, for a better understanding.

    – Alex Mamo
    Feb 19 '18 at 11:05













    3














    To remove the ValueEventListener, you can then do this:



    Remove the anonymity of the listener.



    Change the code from this:-



     Ref.addValueEventListener(new ValueEventListener() 
    @Override
    public void onDataChange(DataSnapshot dataSnapshot)



    @Override
    public void onCancelled(DatabaseError databaseError)


    );


    into this:



     ValueEventListener listener= new ValueEventListener() 
    @Override
    public void onDataChange(DataSnapshot dataSnapshot)



    @Override
    public void onCancelled(DatabaseError databaseError)


    );
    Ref.addValueEventListener(listener);



    Now you will be able to remove the listener:



     @Override
    public void onDestroy()
    if (Ref != null && listener != null)
    Ref.removeEventListener(listener);




    You need to remove it, so the listener does not stay running in the other activity lifecycles like onDestroy()






    share|improve this answer





























      3














      To remove the ValueEventListener, you can then do this:



      Remove the anonymity of the listener.



      Change the code from this:-



       Ref.addValueEventListener(new ValueEventListener() 
      @Override
      public void onDataChange(DataSnapshot dataSnapshot)



      @Override
      public void onCancelled(DatabaseError databaseError)


      );


      into this:



       ValueEventListener listener= new ValueEventListener() 
      @Override
      public void onDataChange(DataSnapshot dataSnapshot)



      @Override
      public void onCancelled(DatabaseError databaseError)


      );
      Ref.addValueEventListener(listener);



      Now you will be able to remove the listener:



       @Override
      public void onDestroy()
      if (Ref != null && listener != null)
      Ref.removeEventListener(listener);




      You need to remove it, so the listener does not stay running in the other activity lifecycles like onDestroy()






      share|improve this answer



























        3












        3








        3







        To remove the ValueEventListener, you can then do this:



        Remove the anonymity of the listener.



        Change the code from this:-



         Ref.addValueEventListener(new ValueEventListener() 
        @Override
        public void onDataChange(DataSnapshot dataSnapshot)



        @Override
        public void onCancelled(DatabaseError databaseError)


        );


        into this:



         ValueEventListener listener= new ValueEventListener() 
        @Override
        public void onDataChange(DataSnapshot dataSnapshot)



        @Override
        public void onCancelled(DatabaseError databaseError)


        );
        Ref.addValueEventListener(listener);



        Now you will be able to remove the listener:



         @Override
        public void onDestroy()
        if (Ref != null && listener != null)
        Ref.removeEventListener(listener);




        You need to remove it, so the listener does not stay running in the other activity lifecycles like onDestroy()






        share|improve this answer















        To remove the ValueEventListener, you can then do this:



        Remove the anonymity of the listener.



        Change the code from this:-



         Ref.addValueEventListener(new ValueEventListener() 
        @Override
        public void onDataChange(DataSnapshot dataSnapshot)



        @Override
        public void onCancelled(DatabaseError databaseError)


        );


        into this:



         ValueEventListener listener= new ValueEventListener() 
        @Override
        public void onDataChange(DataSnapshot dataSnapshot)



        @Override
        public void onCancelled(DatabaseError databaseError)


        );
        Ref.addValueEventListener(listener);



        Now you will be able to remove the listener:



         @Override
        public void onDestroy()
        if (Ref != null && listener != null)
        Ref.removeEventListener(listener);




        You need to remove it, so the listener does not stay running in the other activity lifecycles like onDestroy()







        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Feb 19 '18 at 9:24









        Sreekanth Karumanaghat

        2,01912956




        2,01912956










        answered Feb 19 '18 at 7:43









        Peter HaddadPeter Haddad

        20.5k94157




        20.5k94157



























            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%2f48861350%2fshould-i-actually-remove-the-valueeventlistener%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







            這個網誌中的熱門文章

            Barbados

            How to read a connectionString WITH PROVIDER in .NET Core?

            Node.js Script on GitHub Pages or Amazon S3