Should I actually remove the valueEventListener?
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
add a comment |
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
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 inonStop()
oronDestroy()
.
– ADM
Feb 19 '18 at 7:34
add a comment |
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
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
android multithreading firebase firebase-realtime-database
edited Feb 19 '18 at 21:33
Peter Haddad
20.6k94257
20.6k94257
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 inonStop()
oronDestroy()
.
– ADM
Feb 19 '18 at 7:34
add a comment |
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 inonStop()
oronDestroy()
.
– 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
add a comment |
2 Answers
2
active
oldest
votes
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:
- If you have added the listener in
onStart
you have to remove it inonStop
. - If you have added the listener in
onResume
you have to remove it inonPause
. - If you have added the listener in
onCreate
you have to remove it inonDestroy
.
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.
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
add a comment |
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()
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%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
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:
- If you have added the listener in
onStart
you have to remove it inonStop
. - If you have added the listener in
onResume
you have to remove it inonPause
. - If you have added the listener in
onCreate
you have to remove it inonDestroy
.
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.
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
add a comment |
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:
- If you have added the listener in
onStart
you have to remove it inonStop
. - If you have added the listener in
onResume
you have to remove it inonPause
. - If you have added the listener in
onCreate
you have to remove it inonDestroy
.
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.
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
add a comment |
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:
- If you have added the listener in
onStart
you have to remove it inonStop
. - If you have added the listener in
onResume
you have to remove it inonPause
. - If you have added the listener in
onCreate
you have to remove it inonDestroy
.
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.
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:
- If you have added the listener in
onStart
you have to remove it inonStop
. - If you have added the listener in
onResume
you have to remove it inonPause
. - If you have added the listener in
onCreate
you have to remove it inonDestroy
.
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.
edited Aug 1 '18 at 16:31
JGuo
632522
632522
answered Feb 19 '18 at 9:22
Alex MamoAlex Mamo
41.5k72859
41.5k72859
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
add a comment |
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
add a comment |
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()
add a comment |
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()
add a comment |
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()
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()
edited Feb 19 '18 at 9:24
Sreekanth Karumanaghat
2,01912956
2,01912956
answered Feb 19 '18 at 7:43
Peter HaddadPeter Haddad
20.6k94257
20.6k94257
add a comment |
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%2f48861350%2fshould-i-actually-remove-the-valueeventlistener%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
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()
oronDestroy()
.– ADM
Feb 19 '18 at 7:34