Firebase variable not updating











up vote
1
down vote

favorite












I'm a bit puzzled by strange problem.



I'm trying to access a value on a Firebase database.



final long time = {0};
firebaseAuth = FirebaseAuth.getInstance();
email = FirebaseAuth.getInstance().getCurrentUser().getEmail();
DocumentReference docRef = db.collection("location_times").document(email);
docRef.get().addOnCompleteListener(new OnCompleteListener<DocumentSnapshot>() {
@Override
public void onComplete(@NonNull Task<DocumentSnapshot> task) {
if (task.isSuccessful()) {
DocumentSnapshot document = task.getResult();
if (document.exists()) {
Log.e(TAG, "DocumentSnapshot data: " + document.get("time"));
time[0] = (long) document.get("time");
Log.e(TAG, "Location frequency is " + time[0] + " seconds.");

} else {
Log.e(TAG, "No such document");
}
} else {
Log.e(TAG, "get failed with ", task.getException());
}
}
});

Log.e(TAG, "Time is: " + time[0]);


The time variable is what I want to access.



The Logcat shows the following:



Location frequency is 5 seconds.
Time is: 0


What seems to be the problem? It seems to access the database fine, but the variable isn't updated. What am I doing wrong?



Thanks in advance!










share|improve this question




















  • 1




    In addition to @ManishParajapati's answer, see stackoverflow.com/a/46387410, stackoverflow.com/q/33203379, stackoverflow.com/a/38460676, stackoverflow.com/a/50435519 and quite a few more.
    – Frank van Puffelen
    Nov 10 at 15:16










  • Never thought this would be the problem. I'm still learning I guess :) Thanks!
    – dj.rodrigues
    Nov 10 at 15:30

















up vote
1
down vote

favorite












I'm a bit puzzled by strange problem.



I'm trying to access a value on a Firebase database.



final long time = {0};
firebaseAuth = FirebaseAuth.getInstance();
email = FirebaseAuth.getInstance().getCurrentUser().getEmail();
DocumentReference docRef = db.collection("location_times").document(email);
docRef.get().addOnCompleteListener(new OnCompleteListener<DocumentSnapshot>() {
@Override
public void onComplete(@NonNull Task<DocumentSnapshot> task) {
if (task.isSuccessful()) {
DocumentSnapshot document = task.getResult();
if (document.exists()) {
Log.e(TAG, "DocumentSnapshot data: " + document.get("time"));
time[0] = (long) document.get("time");
Log.e(TAG, "Location frequency is " + time[0] + " seconds.");

} else {
Log.e(TAG, "No such document");
}
} else {
Log.e(TAG, "get failed with ", task.getException());
}
}
});

Log.e(TAG, "Time is: " + time[0]);


The time variable is what I want to access.



The Logcat shows the following:



Location frequency is 5 seconds.
Time is: 0


What seems to be the problem? It seems to access the database fine, but the variable isn't updated. What am I doing wrong?



Thanks in advance!










share|improve this question




















  • 1




    In addition to @ManishParajapati's answer, see stackoverflow.com/a/46387410, stackoverflow.com/q/33203379, stackoverflow.com/a/38460676, stackoverflow.com/a/50435519 and quite a few more.
    – Frank van Puffelen
    Nov 10 at 15:16










  • Never thought this would be the problem. I'm still learning I guess :) Thanks!
    – dj.rodrigues
    Nov 10 at 15:30















up vote
1
down vote

favorite









up vote
1
down vote

favorite











I'm a bit puzzled by strange problem.



I'm trying to access a value on a Firebase database.



final long time = {0};
firebaseAuth = FirebaseAuth.getInstance();
email = FirebaseAuth.getInstance().getCurrentUser().getEmail();
DocumentReference docRef = db.collection("location_times").document(email);
docRef.get().addOnCompleteListener(new OnCompleteListener<DocumentSnapshot>() {
@Override
public void onComplete(@NonNull Task<DocumentSnapshot> task) {
if (task.isSuccessful()) {
DocumentSnapshot document = task.getResult();
if (document.exists()) {
Log.e(TAG, "DocumentSnapshot data: " + document.get("time"));
time[0] = (long) document.get("time");
Log.e(TAG, "Location frequency is " + time[0] + " seconds.");

} else {
Log.e(TAG, "No such document");
}
} else {
Log.e(TAG, "get failed with ", task.getException());
}
}
});

Log.e(TAG, "Time is: " + time[0]);


The time variable is what I want to access.



The Logcat shows the following:



Location frequency is 5 seconds.
Time is: 0


What seems to be the problem? It seems to access the database fine, but the variable isn't updated. What am I doing wrong?



Thanks in advance!










share|improve this question















I'm a bit puzzled by strange problem.



I'm trying to access a value on a Firebase database.



final long time = {0};
firebaseAuth = FirebaseAuth.getInstance();
email = FirebaseAuth.getInstance().getCurrentUser().getEmail();
DocumentReference docRef = db.collection("location_times").document(email);
docRef.get().addOnCompleteListener(new OnCompleteListener<DocumentSnapshot>() {
@Override
public void onComplete(@NonNull Task<DocumentSnapshot> task) {
if (task.isSuccessful()) {
DocumentSnapshot document = task.getResult();
if (document.exists()) {
Log.e(TAG, "DocumentSnapshot data: " + document.get("time"));
time[0] = (long) document.get("time");
Log.e(TAG, "Location frequency is " + time[0] + " seconds.");

} else {
Log.e(TAG, "No such document");
}
} else {
Log.e(TAG, "get failed with ", task.getException());
}
}
});

Log.e(TAG, "Time is: " + time[0]);


The time variable is what I want to access.



The Logcat shows the following:



Location frequency is 5 seconds.
Time is: 0


What seems to be the problem? It seems to access the database fine, but the variable isn't updated. What am I doing wrong?



Thanks in advance!







android firebase firebase-realtime-database






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 10 at 15:12









Frank van Puffelen

218k25361385




218k25361385










asked Nov 10 at 14:37









dj.rodrigues

184




184








  • 1




    In addition to @ManishParajapati's answer, see stackoverflow.com/a/46387410, stackoverflow.com/q/33203379, stackoverflow.com/a/38460676, stackoverflow.com/a/50435519 and quite a few more.
    – Frank van Puffelen
    Nov 10 at 15:16










  • Never thought this would be the problem. I'm still learning I guess :) Thanks!
    – dj.rodrigues
    Nov 10 at 15:30
















  • 1




    In addition to @ManishParajapati's answer, see stackoverflow.com/a/46387410, stackoverflow.com/q/33203379, stackoverflow.com/a/38460676, stackoverflow.com/a/50435519 and quite a few more.
    – Frank van Puffelen
    Nov 10 at 15:16










  • Never thought this would be the problem. I'm still learning I guess :) Thanks!
    – dj.rodrigues
    Nov 10 at 15:30










1




1




In addition to @ManishParajapati's answer, see stackoverflow.com/a/46387410, stackoverflow.com/q/33203379, stackoverflow.com/a/38460676, stackoverflow.com/a/50435519 and quite a few more.
– Frank van Puffelen
Nov 10 at 15:16




In addition to @ManishParajapati's answer, see stackoverflow.com/a/46387410, stackoverflow.com/q/33203379, stackoverflow.com/a/38460676, stackoverflow.com/a/50435519 and quite a few more.
– Frank van Puffelen
Nov 10 at 15:16












Never thought this would be the problem. I'm still learning I guess :) Thanks!
– dj.rodrigues
Nov 10 at 15:30






Never thought this would be the problem. I'm still learning I guess :) Thanks!
– dj.rodrigues
Nov 10 at 15:30














1 Answer
1






active

oldest

votes

















up vote
2
down vote



accepted










It is 0 because it is not updated yet, as firebase runs a async task to complete the listeners.



You are accessing Log.e(TAG, "Time is: " + time[0]); line of code outside onComplete listener. This line of code is executed before even the firebase has completed its fetching. You can implement a method call and access the Time from there.



final long time = {0};
firebaseAuth = FirebaseAuth.getInstance();
email = FirebaseAuth.getInstance().getCurrentUser().getEmail();
DocumentReference docRef = db.collection("location_times").document(email);
docRef.get().addOnCompleteListener(new OnCompleteListener<DocumentSnapshot>() {
@Override
public void onComplete(@NonNull Task<DocumentSnapshot> task) {
if (task.isSuccessful()) {
DocumentSnapshot document = task.getResult();
if (document.exists()) {
Log.e(TAG, "DocumentSnapshot data: " + document.get("time"));
time[0] = (long) document.get("time");
Log.e(TAG, "Location frequency is " + time[0] + " seconds.");
showTime(); // Added a method call
} else {
Log.e(TAG, "No such document");
}
} else {
Log.e(TAG, "get failed with ", task.getException());
}
}
});

public void showTime(){ // perform your action after the firebase task is completed
Log.e(TAG, "Time is: " + time[0]);
}





share|improve this answer























  • I still have to access that variable immediately after the Firebase task. Where can I call that new method?
    – dj.rodrigues
    Nov 10 at 14:58






  • 1




    I have added a new method showTime() in onCompleteListener, it will call that method immediately once the firebase task is completed and your if condition is met.
    – ManishPrajapati
    Nov 10 at 15:03










  • It works now! Thanks!
    – dj.rodrigues
    Nov 10 at 15:30











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',
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%2f53240001%2ffirebase-variable-not-updating%23new-answer', 'question_page');
}
);

Post as a guest
































1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
2
down vote



accepted










It is 0 because it is not updated yet, as firebase runs a async task to complete the listeners.



You are accessing Log.e(TAG, "Time is: " + time[0]); line of code outside onComplete listener. This line of code is executed before even the firebase has completed its fetching. You can implement a method call and access the Time from there.



final long time = {0};
firebaseAuth = FirebaseAuth.getInstance();
email = FirebaseAuth.getInstance().getCurrentUser().getEmail();
DocumentReference docRef = db.collection("location_times").document(email);
docRef.get().addOnCompleteListener(new OnCompleteListener<DocumentSnapshot>() {
@Override
public void onComplete(@NonNull Task<DocumentSnapshot> task) {
if (task.isSuccessful()) {
DocumentSnapshot document = task.getResult();
if (document.exists()) {
Log.e(TAG, "DocumentSnapshot data: " + document.get("time"));
time[0] = (long) document.get("time");
Log.e(TAG, "Location frequency is " + time[0] + " seconds.");
showTime(); // Added a method call
} else {
Log.e(TAG, "No such document");
}
} else {
Log.e(TAG, "get failed with ", task.getException());
}
}
});

public void showTime(){ // perform your action after the firebase task is completed
Log.e(TAG, "Time is: " + time[0]);
}





share|improve this answer























  • I still have to access that variable immediately after the Firebase task. Where can I call that new method?
    – dj.rodrigues
    Nov 10 at 14:58






  • 1




    I have added a new method showTime() in onCompleteListener, it will call that method immediately once the firebase task is completed and your if condition is met.
    – ManishPrajapati
    Nov 10 at 15:03










  • It works now! Thanks!
    – dj.rodrigues
    Nov 10 at 15:30















up vote
2
down vote



accepted










It is 0 because it is not updated yet, as firebase runs a async task to complete the listeners.



You are accessing Log.e(TAG, "Time is: " + time[0]); line of code outside onComplete listener. This line of code is executed before even the firebase has completed its fetching. You can implement a method call and access the Time from there.



final long time = {0};
firebaseAuth = FirebaseAuth.getInstance();
email = FirebaseAuth.getInstance().getCurrentUser().getEmail();
DocumentReference docRef = db.collection("location_times").document(email);
docRef.get().addOnCompleteListener(new OnCompleteListener<DocumentSnapshot>() {
@Override
public void onComplete(@NonNull Task<DocumentSnapshot> task) {
if (task.isSuccessful()) {
DocumentSnapshot document = task.getResult();
if (document.exists()) {
Log.e(TAG, "DocumentSnapshot data: " + document.get("time"));
time[0] = (long) document.get("time");
Log.e(TAG, "Location frequency is " + time[0] + " seconds.");
showTime(); // Added a method call
} else {
Log.e(TAG, "No such document");
}
} else {
Log.e(TAG, "get failed with ", task.getException());
}
}
});

public void showTime(){ // perform your action after the firebase task is completed
Log.e(TAG, "Time is: " + time[0]);
}





share|improve this answer























  • I still have to access that variable immediately after the Firebase task. Where can I call that new method?
    – dj.rodrigues
    Nov 10 at 14:58






  • 1




    I have added a new method showTime() in onCompleteListener, it will call that method immediately once the firebase task is completed and your if condition is met.
    – ManishPrajapati
    Nov 10 at 15:03










  • It works now! Thanks!
    – dj.rodrigues
    Nov 10 at 15:30













up vote
2
down vote



accepted







up vote
2
down vote



accepted






It is 0 because it is not updated yet, as firebase runs a async task to complete the listeners.



You are accessing Log.e(TAG, "Time is: " + time[0]); line of code outside onComplete listener. This line of code is executed before even the firebase has completed its fetching. You can implement a method call and access the Time from there.



final long time = {0};
firebaseAuth = FirebaseAuth.getInstance();
email = FirebaseAuth.getInstance().getCurrentUser().getEmail();
DocumentReference docRef = db.collection("location_times").document(email);
docRef.get().addOnCompleteListener(new OnCompleteListener<DocumentSnapshot>() {
@Override
public void onComplete(@NonNull Task<DocumentSnapshot> task) {
if (task.isSuccessful()) {
DocumentSnapshot document = task.getResult();
if (document.exists()) {
Log.e(TAG, "DocumentSnapshot data: " + document.get("time"));
time[0] = (long) document.get("time");
Log.e(TAG, "Location frequency is " + time[0] + " seconds.");
showTime(); // Added a method call
} else {
Log.e(TAG, "No such document");
}
} else {
Log.e(TAG, "get failed with ", task.getException());
}
}
});

public void showTime(){ // perform your action after the firebase task is completed
Log.e(TAG, "Time is: " + time[0]);
}





share|improve this answer














It is 0 because it is not updated yet, as firebase runs a async task to complete the listeners.



You are accessing Log.e(TAG, "Time is: " + time[0]); line of code outside onComplete listener. This line of code is executed before even the firebase has completed its fetching. You can implement a method call and access the Time from there.



final long time = {0};
firebaseAuth = FirebaseAuth.getInstance();
email = FirebaseAuth.getInstance().getCurrentUser().getEmail();
DocumentReference docRef = db.collection("location_times").document(email);
docRef.get().addOnCompleteListener(new OnCompleteListener<DocumentSnapshot>() {
@Override
public void onComplete(@NonNull Task<DocumentSnapshot> task) {
if (task.isSuccessful()) {
DocumentSnapshot document = task.getResult();
if (document.exists()) {
Log.e(TAG, "DocumentSnapshot data: " + document.get("time"));
time[0] = (long) document.get("time");
Log.e(TAG, "Location frequency is " + time[0] + " seconds.");
showTime(); // Added a method call
} else {
Log.e(TAG, "No such document");
}
} else {
Log.e(TAG, "get failed with ", task.getException());
}
}
});

public void showTime(){ // perform your action after the firebase task is completed
Log.e(TAG, "Time is: " + time[0]);
}






share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 10 at 15:04

























answered Nov 10 at 14:49









ManishPrajapati

166212




166212












  • I still have to access that variable immediately after the Firebase task. Where can I call that new method?
    – dj.rodrigues
    Nov 10 at 14:58






  • 1




    I have added a new method showTime() in onCompleteListener, it will call that method immediately once the firebase task is completed and your if condition is met.
    – ManishPrajapati
    Nov 10 at 15:03










  • It works now! Thanks!
    – dj.rodrigues
    Nov 10 at 15:30


















  • I still have to access that variable immediately after the Firebase task. Where can I call that new method?
    – dj.rodrigues
    Nov 10 at 14:58






  • 1




    I have added a new method showTime() in onCompleteListener, it will call that method immediately once the firebase task is completed and your if condition is met.
    – ManishPrajapati
    Nov 10 at 15:03










  • It works now! Thanks!
    – dj.rodrigues
    Nov 10 at 15:30
















I still have to access that variable immediately after the Firebase task. Where can I call that new method?
– dj.rodrigues
Nov 10 at 14:58




I still have to access that variable immediately after the Firebase task. Where can I call that new method?
– dj.rodrigues
Nov 10 at 14:58




1




1




I have added a new method showTime() in onCompleteListener, it will call that method immediately once the firebase task is completed and your if condition is met.
– ManishPrajapati
Nov 10 at 15:03




I have added a new method showTime() in onCompleteListener, it will call that method immediately once the firebase task is completed and your if condition is met.
– ManishPrajapati
Nov 10 at 15:03












It works now! Thanks!
– dj.rodrigues
Nov 10 at 15:30




It works now! Thanks!
– dj.rodrigues
Nov 10 at 15:30


















 

draft saved


draft discarded



















































 


draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53240001%2ffirebase-variable-not-updating%23new-answer', 'question_page');
}
);

Post as a guest




















































































Popular posts from this blog

Bressuire

Vorschmack

Quarantine