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
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)
)
add a comment |
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)
)
add a comment |
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)
)
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)
)
edited Mar 8 at 6:44
Tsabary
asked Mar 8 at 6:37
TsabaryTsabary
7110
7110
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
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.
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
add a comment |
My problem was in using addValueEventListener instead of addListenerForSingleValueEvent which I woudl assume as implied by name would listen once and let go.
You should accept Pamela Hill's answer and close this issue.
– shkschneider
Mar 8 at 12:53
add a comment |
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
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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.
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
add a comment |
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.
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
add a comment |
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.
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.
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
add a comment |
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
add a comment |
My problem was in using addValueEventListener instead of addListenerForSingleValueEvent which I woudl assume as implied by name would listen once and let go.
You should accept Pamela Hill's answer and close this issue.
– shkschneider
Mar 8 at 12:53
add a comment |
My problem was in using addValueEventListener instead of addListenerForSingleValueEvent which I woudl assume as implied by name would listen once and let go.
You should accept Pamela Hill's answer and close this issue.
– shkschneider
Mar 8 at 12:53
add a comment |
My problem was in using addValueEventListener instead of addListenerForSingleValueEvent which I woudl assume as implied by name would listen once and let go.
My problem was in using addValueEventListener instead of addListenerForSingleValueEvent which I woudl assume as implied by name would listen once and let go.
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
add a comment |
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
add a comment |
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.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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