【发布时间】:2018-11-10 23:12:09
【问题描述】:
我对奇怪的问题有点困惑。
我正在尝试访问 Firebase 数据库中的值。
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]);
time 变量是我想要访问的。
Logcat 显示如下:
Location frequency is 5 seconds.
Time is: 0
似乎是什么问题?似乎可以正常访问数据库,但变量没有更新。我做错了什么?
提前致谢!
【问题讨论】:
-
除了@ManishParajapati 的回答,请参阅stackoverflow.com/a/46387410、stackoverflow.com/q/33203379、stackoverflow.com/a/38460676、stackoverflow.com/a/50435519 等等。
-
从没想过这会是问题所在。我想我还在学习 :) 谢谢!
标签: android firebase firebase-realtime-database