Upvote/downvote function keeps running for ever even though it is set in an on click listenerKeep a Service running even when phone is asleep?Firebase: keep listening to ChildEventListener though app exitsGet children using orderByChild-equalsTo in Firebase-AndroidAdding Social Media Share Logic From Firebase in Androidfunction start even though its not calledUpdated global variables does not reflect inside ValueEventListener's onCancelled methodAndroid - Keep Listener with Firebase function out of the classHow to implement a client responsive upvote/downvote system with firebase functionsHow to set click listener to this Buttonhow to set click listener on notification banner when app is in foreground

Can the Supreme Court overturn an impeachment?

How must one send away the mother bird?

Is there a conventional notation or name for the slip angle?

Should I stop contributing to retirement accounts?

How do I extrude a face to a single vertex

Why do IPv6 unique local addresses have to have a /48 prefix?

Do Legal Documents Require Signing In Standard Pen Colors?

What is this type of notehead called?

Query about absorption line spectra

Two-sided logarithm inequality

How do I repair my stair bannister?

Is camera lens focus an exact point or a range?

Bob has never been a M before

Gibbs free energy in standard state vs. equilibrium

anything or something to eat

Can I use my Chinese passport to enter China after I acquired another citizenship?

How do ground effect vehicles perform turns?

Why does Async/Await work properly when the loop is inside the async function and not the other way around?

Customize circled numbers

Has Darkwing Duck ever met Scrooge McDuck?

Is possible to search in vim history?

Why does the Sun have different day lengths, but not the gas giants?

What linear sensor for a keyboard?

Diode in opposite direction?



Upvote/downvote function keeps running for ever even though it is set in an on click listener


Keep a Service running even when phone is asleep?Firebase: keep listening to ChildEventListener though app exitsGet children using orderByChild-equalsTo in Firebase-AndroidAdding Social Media Share Logic From Firebase in Androidfunction start even though its not calledUpdated global variables does not reflect inside ValueEventListener's onCancelled methodAndroid - Keep Listener with Firebase function out of the classHow to implement a client responsive upvote/downvote system with firebase functionsHow to set click listener to this Buttonhow to set click listener on notification banner when app is in foreground













0















I've built a function to control the upvotes and downvotes on replies. Exactly like it works on SO.



It works perfectly fins - almost.



If I call it once (by clicking upvote or downvote), it is completely fine, but if I call it again (set it to 0 from -1 or 1), it starts running forever bouncing through -1, 0 and 1 (I am the only user so it doesn't get beyond 1 vote).



I can't understand what is causing this behavior. I'm thinking maybe it has something to do with the fact that it is inside a listener so the value change keeps igniting the function? If that's the case, how do I get the value I need without a listener? I'm not sure.. Help?



 private fun executeVote(vote: String) 


val refVotes = FirebaseDatabase.getInstance().getReference("/votes/$answerId")
refVotes.addValueEventListener(object : ValueEventListener
override fun onCancelled(p0: DatabaseError)



override fun onDataChange(p0: DataSnapshot)
val refUserVote = FirebaseDatabase.getInstance().getReference("/votes/$answerId/$uid")

if (p0.hasChild("$uid"))
var voteValue = 0
refUserVote.addListenerForSingleValueEvent(object : ValueEventListener
override fun onCancelled(p0: DatabaseError)



override fun onDataChange(p0: DataSnapshot)
voteValue = p0.getValue().toString().toInt()

when (voteValue)

1 ->
when (vote)
"up" -> refUserVote.setValue(1)
"down" -> refUserVote.setValue(0)
else -> refUserVote.setValue(0)



0 ->
when (vote)
"up" -> refUserVote.setValue(1)
"down" -> refUserVote.setValue(-1)
else -> refUserVote.setValue(0)




-1 ->
when (vote)
"up" -> refUserVote.setValue(0)
"down" -> refUserVote.setValue(-1)
else -> refUserVote.setValue(0)







)


else
when (vote)
"up" -> refUserVote.setValue(1)
"down" -> refUserVote.setValue(-1)
else -> refUserVote.setValue(0)





)











share|improve this question




























    0















    I've built a function to control the upvotes and downvotes on replies. Exactly like it works on SO.



    It works perfectly fins - almost.



    If I call it once (by clicking upvote or downvote), it is completely fine, but if I call it again (set it to 0 from -1 or 1), it starts running forever bouncing through -1, 0 and 1 (I am the only user so it doesn't get beyond 1 vote).



    I can't understand what is causing this behavior. I'm thinking maybe it has something to do with the fact that it is inside a listener so the value change keeps igniting the function? If that's the case, how do I get the value I need without a listener? I'm not sure.. Help?



     private fun executeVote(vote: String) 


    val refVotes = FirebaseDatabase.getInstance().getReference("/votes/$answerId")
    refVotes.addValueEventListener(object : ValueEventListener
    override fun onCancelled(p0: DatabaseError)



    override fun onDataChange(p0: DataSnapshot)
    val refUserVote = FirebaseDatabase.getInstance().getReference("/votes/$answerId/$uid")

    if (p0.hasChild("$uid"))
    var voteValue = 0
    refUserVote.addListenerForSingleValueEvent(object : ValueEventListener
    override fun onCancelled(p0: DatabaseError)



    override fun onDataChange(p0: DataSnapshot)
    voteValue = p0.getValue().toString().toInt()

    when (voteValue)

    1 ->
    when (vote)
    "up" -> refUserVote.setValue(1)
    "down" -> refUserVote.setValue(0)
    else -> refUserVote.setValue(0)



    0 ->
    when (vote)
    "up" -> refUserVote.setValue(1)
    "down" -> refUserVote.setValue(-1)
    else -> refUserVote.setValue(0)




    -1 ->
    when (vote)
    "up" -> refUserVote.setValue(0)
    "down" -> refUserVote.setValue(-1)
    else -> refUserVote.setValue(0)







    )


    else
    when (vote)
    "up" -> refUserVote.setValue(1)
    "down" -> refUserVote.setValue(-1)
    else -> refUserVote.setValue(0)





    )











    share|improve this question


























      0












      0








      0








      I've built a function to control the upvotes and downvotes on replies. Exactly like it works on SO.



      It works perfectly fins - almost.



      If I call it once (by clicking upvote or downvote), it is completely fine, but if I call it again (set it to 0 from -1 or 1), it starts running forever bouncing through -1, 0 and 1 (I am the only user so it doesn't get beyond 1 vote).



      I can't understand what is causing this behavior. I'm thinking maybe it has something to do with the fact that it is inside a listener so the value change keeps igniting the function? If that's the case, how do I get the value I need without a listener? I'm not sure.. Help?



       private fun executeVote(vote: String) 


      val refVotes = FirebaseDatabase.getInstance().getReference("/votes/$answerId")
      refVotes.addValueEventListener(object : ValueEventListener
      override fun onCancelled(p0: DatabaseError)



      override fun onDataChange(p0: DataSnapshot)
      val refUserVote = FirebaseDatabase.getInstance().getReference("/votes/$answerId/$uid")

      if (p0.hasChild("$uid"))
      var voteValue = 0
      refUserVote.addListenerForSingleValueEvent(object : ValueEventListener
      override fun onCancelled(p0: DatabaseError)



      override fun onDataChange(p0: DataSnapshot)
      voteValue = p0.getValue().toString().toInt()

      when (voteValue)

      1 ->
      when (vote)
      "up" -> refUserVote.setValue(1)
      "down" -> refUserVote.setValue(0)
      else -> refUserVote.setValue(0)



      0 ->
      when (vote)
      "up" -> refUserVote.setValue(1)
      "down" -> refUserVote.setValue(-1)
      else -> refUserVote.setValue(0)




      -1 ->
      when (vote)
      "up" -> refUserVote.setValue(0)
      "down" -> refUserVote.setValue(-1)
      else -> refUserVote.setValue(0)







      )


      else
      when (vote)
      "up" -> refUserVote.setValue(1)
      "down" -> refUserVote.setValue(-1)
      else -> refUserVote.setValue(0)





      )











      share|improve this question
















      I've built a function to control the upvotes and downvotes on replies. Exactly like it works on SO.



      It works perfectly fins - almost.



      If I call it once (by clicking upvote or downvote), it is completely fine, but if I call it again (set it to 0 from -1 or 1), it starts running forever bouncing through -1, 0 and 1 (I am the only user so it doesn't get beyond 1 vote).



      I can't understand what is causing this behavior. I'm thinking maybe it has something to do with the fact that it is inside a listener so the value change keeps igniting the function? If that's the case, how do I get the value I need without a listener? I'm not sure.. Help?



       private fun executeVote(vote: String) 


      val refVotes = FirebaseDatabase.getInstance().getReference("/votes/$answerId")
      refVotes.addValueEventListener(object : ValueEventListener
      override fun onCancelled(p0: DatabaseError)



      override fun onDataChange(p0: DataSnapshot)
      val refUserVote = FirebaseDatabase.getInstance().getReference("/votes/$answerId/$uid")

      if (p0.hasChild("$uid"))
      var voteValue = 0
      refUserVote.addListenerForSingleValueEvent(object : ValueEventListener
      override fun onCancelled(p0: DatabaseError)



      override fun onDataChange(p0: DataSnapshot)
      voteValue = p0.getValue().toString().toInt()

      when (voteValue)

      1 ->
      when (vote)
      "up" -> refUserVote.setValue(1)
      "down" -> refUserVote.setValue(0)
      else -> refUserVote.setValue(0)



      0 ->
      when (vote)
      "up" -> refUserVote.setValue(1)
      "down" -> refUserVote.setValue(-1)
      else -> refUserVote.setValue(0)




      -1 ->
      when (vote)
      "up" -> refUserVote.setValue(0)
      "down" -> refUserVote.setValue(-1)
      else -> refUserVote.setValue(0)







      )


      else
      when (vote)
      "up" -> refUserVote.setValue(1)
      "down" -> refUserVote.setValue(-1)
      else -> refUserVote.setValue(0)





      )








      android firebase kotlin






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Mar 8 at 6:44







      Tsabary

















      asked Mar 8 at 6:37









      TsabaryTsabary

      7110




      7110






















          2 Answers
          2






          active

          oldest

          votes


















          1














          It makes sense because when you're calling setValue, the onDataChange will be called again, even though you are calling it from that same function.



          When you're about to call setValue, remove the listener, call setValue, add the listener again (this is a typical Java way).



          I would also avoid doing something in the else conditions, it's unneccessarily triggering events.






          share|improve this answer























          • Thank you Pamela, two question: #1. How do I remove the listener and add it? Is their some function to do it? No sure what that means. #2. Should I just delete the else statements?

            – Tsabary
            Mar 8 at 6:52











          • 1. Create the listener and save it as a val. Add the val and remove the val.

            – Pamela Hill
            Mar 8 at 8:48











          • 2. I would remove the else statements, yes, or have empty else blocks :D

            – Pamela Hill
            Mar 8 at 8:49


















          0














          My problem was in using addValueEventListener instead of addListenerForSingleValueEvent which I woudl assume as implied by name would listen once and let go.






          share|improve this answer























          • You should accept Pamela Hill's answer and close this issue.

            – shkschneider
            Mar 8 at 12:53










          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%2f55057939%2fupvote-downvote-function-keeps-running-for-ever-even-though-it-is-set-in-an-on-c%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









          1














          It makes sense because when you're calling setValue, the onDataChange will be called again, even though you are calling it from that same function.



          When you're about to call setValue, remove the listener, call setValue, add the listener again (this is a typical Java way).



          I would also avoid doing something in the else conditions, it's unneccessarily triggering events.






          share|improve this answer























          • Thank you Pamela, two question: #1. How do I remove the listener and add it? Is their some function to do it? No sure what that means. #2. Should I just delete the else statements?

            – Tsabary
            Mar 8 at 6:52











          • 1. Create the listener and save it as a val. Add the val and remove the val.

            – Pamela Hill
            Mar 8 at 8:48











          • 2. I would remove the else statements, yes, or have empty else blocks :D

            – Pamela Hill
            Mar 8 at 8:49















          1














          It makes sense because when you're calling setValue, the onDataChange will be called again, even though you are calling it from that same function.



          When you're about to call setValue, remove the listener, call setValue, add the listener again (this is a typical Java way).



          I would also avoid doing something in the else conditions, it's unneccessarily triggering events.






          share|improve this answer























          • Thank you Pamela, two question: #1. How do I remove the listener and add it? Is their some function to do it? No sure what that means. #2. Should I just delete the else statements?

            – Tsabary
            Mar 8 at 6:52











          • 1. Create the listener and save it as a val. Add the val and remove the val.

            – Pamela Hill
            Mar 8 at 8:48











          • 2. I would remove the else statements, yes, or have empty else blocks :D

            – Pamela Hill
            Mar 8 at 8:49













          1












          1








          1







          It makes sense because when you're calling setValue, the onDataChange will be called again, even though you are calling it from that same function.



          When you're about to call setValue, remove the listener, call setValue, add the listener again (this is a typical Java way).



          I would also avoid doing something in the else conditions, it's unneccessarily triggering events.






          share|improve this answer













          It makes sense because when you're calling setValue, the onDataChange will be called again, even though you are calling it from that same function.



          When you're about to call setValue, remove the listener, call setValue, add the listener again (this is a typical Java way).



          I would also avoid doing something in the else conditions, it's unneccessarily triggering events.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Mar 8 at 6:48









          Pamela HillPamela Hill

          1096




          1096












          • Thank you Pamela, two question: #1. How do I remove the listener and add it? Is their some function to do it? No sure what that means. #2. Should I just delete the else statements?

            – Tsabary
            Mar 8 at 6:52











          • 1. Create the listener and save it as a val. Add the val and remove the val.

            – Pamela Hill
            Mar 8 at 8:48











          • 2. I would remove the else statements, yes, or have empty else blocks :D

            – Pamela Hill
            Mar 8 at 8:49

















          • Thank you Pamela, two question: #1. How do I remove the listener and add it? Is their some function to do it? No sure what that means. #2. Should I just delete the else statements?

            – Tsabary
            Mar 8 at 6:52











          • 1. Create the listener and save it as a val. Add the val and remove the val.

            – Pamela Hill
            Mar 8 at 8:48











          • 2. I would remove the else statements, yes, or have empty else blocks :D

            – Pamela Hill
            Mar 8 at 8:49
















          Thank you Pamela, two question: #1. How do I remove the listener and add it? Is their some function to do it? No sure what that means. #2. Should I just delete the else statements?

          – Tsabary
          Mar 8 at 6:52





          Thank you Pamela, two question: #1. How do I remove the listener and add it? Is their some function to do it? No sure what that means. #2. Should I just delete the else statements?

          – Tsabary
          Mar 8 at 6:52













          1. Create the listener and save it as a val. Add the val and remove the val.

          – Pamela Hill
          Mar 8 at 8:48





          1. Create the listener and save it as a val. Add the val and remove the val.

          – Pamela Hill
          Mar 8 at 8:48













          2. I would remove the else statements, yes, or have empty else blocks :D

          – Pamela Hill
          Mar 8 at 8:49





          2. I would remove the else statements, yes, or have empty else blocks :D

          – Pamela Hill
          Mar 8 at 8:49













          0














          My problem was in using addValueEventListener instead of addListenerForSingleValueEvent which I woudl assume as implied by name would listen once and let go.






          share|improve this answer























          • You should accept Pamela Hill's answer and close this issue.

            – shkschneider
            Mar 8 at 12:53















          0














          My problem was in using addValueEventListener instead of addListenerForSingleValueEvent which I woudl assume as implied by name would listen once and let go.






          share|improve this answer























          • You should accept Pamela Hill's answer and close this issue.

            – shkschneider
            Mar 8 at 12:53













          0












          0








          0







          My problem was in using addValueEventListener instead of addListenerForSingleValueEvent which I woudl assume as implied by name would listen once and let go.






          share|improve this answer













          My problem was in using addValueEventListener instead of addListenerForSingleValueEvent which I woudl assume as implied by name would listen once and let go.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Mar 8 at 6:58









          TsabaryTsabary

          7110




          7110












          • You should accept Pamela Hill's answer and close this issue.

            – shkschneider
            Mar 8 at 12:53

















          • You should accept Pamela Hill's answer and close this issue.

            – shkschneider
            Mar 8 at 12:53
















          You should accept Pamela Hill's answer and close this issue.

          – shkschneider
          Mar 8 at 12:53





          You should accept Pamela Hill's answer and close this issue.

          – shkschneider
          Mar 8 at 12:53

















          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%2f55057939%2fupvote-downvote-function-keeps-running-for-ever-even-though-it-is-set-in-an-on-c%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

          How to get text form Clipboard with JavaScript in Firefox 56?How to validate an email address in JavaScript?How do JavaScript closures work?How do I remove a property from a JavaScript object?How do you get a timestamp in JavaScript?How do I copy to the clipboard in JavaScript?How do I include a JavaScript file in another JavaScript file?Get the current URL with JavaScript?How to replace all occurrences of a string in JavaScriptHow to check whether a string contains a substring in JavaScript?How do I remove a particular element from an array in JavaScript?

          Can't initialize raids on a new ASUS Prime B360M-A motherboard2019 Community Moderator ElectionSimilar to RAID config yet more like mirroring solution?Can't get motherboard serial numberWhy does the BIOS entry point start with a WBINVD instruction?UEFI performance Asus Maximus V Extreme

          List of MPs elected to the English parliament in 1640 (April) Contents List of constituencies and members See also Notes References Navigation menueNational Archives – The Glynde Place ArchivesCobbett's Parliamentary history of England, from the Norman Conquest in 1066 to the year 1803'Aldermen in Parliament', The Aldermen of the City of London: Temp. Henry III – 1912onepage&q&f&#61, false 229