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!
android firebase firebase-realtime-database
add a comment |
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!
android firebase firebase-realtime-database
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
add a comment |
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!
android firebase firebase-realtime-database
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
android firebase firebase-realtime-database
edited Nov 10 at 15:12
Frank van Puffelen
218k25361386
218k25361386
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
add a comment |
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
add a comment |
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]);
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
add a comment |
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]);
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
add a comment |
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]);
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
add a comment |
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]);
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]);
edited Nov 10 at 15:04
answered Nov 10 at 14:49
ManishPrajapati
211313
211313
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
add a comment |
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
add a comment |
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%2f53240001%2ffirebase-variable-not-updating%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
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