I am try to import a single field value from a document.The problem here is code is not printing giving the error.I have check the code from firestore documentation too.
I tried using OnSuccessListener too,yet no result.
private FirebaseFirestore db,db1;
Map<String,Object > number=new HashMap<>();
db = FirebaseFirestore.getInstance();
DocumentReference docRef = db.collection("goals").document(email);
docRef.get().addOnCompleteListener(new OnCompleteListener<DocumentSnapshot>() {
#Override
public void onComplete(#NonNull Task<DocumentSnapshot> task) {
String TAG="lol";
System.out.println("IT CAME Here") ;
if (task.isSuccessful()) {
DocumentSnapshot document = task.getResult();
if (document.exists()) {
Log.d(TAG, "DocumentSnapshot data: " + document.getData());
number=document.getData();
name = document.getString("number");
} else {
Log.d(TAG, "No such document");
}
} else {
System.out.println("get failed with ");
Log.d(TAG, "get failed with ", task.getException());
}
}
});
Not even the print statements in the else loops are being printed.It seems like its skipping that block of code
Related
I'm creating an app in the android studio IDE and I want to display the inserted data in a text box
but the problem is I don't know how to retrieve the data that I inserted in the driver field. The driver field is a map, Is there a way to retrieve the data insert in the driver field? This is my firebase fire store look like, I want to get the tricycle number data.
can anyone give me example on how to retrieve a data in a map collection field?
Following the documentation on getting data from Firestore, you can get a DataSnapshot object of the data at that location. Then using DocumentSnapshot#get() to get the value of an individual field.
This can be done using:
FirebaseFirestore db = FirebaseFirestore.getInstance();
DocumentReference driverDocRef = db.collection("Driver Locations")
.document(driverId);
driverDocRef.get()
.addOnCompleteListener(new OnCompleteListener<DocumentSnapshot>() {
#Override
public void onComplete(#NonNull Task<DocumentSnapshot> task) {
if (task.isSuccessful()) {
DocumentSnapshot document = task.getResult();
if (document.exists()) {
Log.d(TAG, "Driver #" + driverId + "'s Tricycle Number is " + document.get("driver.tricyclenumber", String.class));
} else {
Log.d(TAG, "No such document");
}
} else {
Log.d(TAG, "get failed with ", task.getException());
}
}
});
To make this into a function that you can reuse elsewhere, you can make use of Task#onSuccessTask() to chain tasks together.
One such implementation of this would be:
public Task<String> getDriverTricycleNumber(String driverId) {
FirebaseFirestore db = FirebaseFirestore.getInstance();
DocumentReference driverDocRef = db.collection("Driver Locations")
.document(driverId);
return driverDocRef.get()
.onSuccessTask(new SuccessContinuation<DocumentSnapshot, String>() {
#NonNull
#Override
public Task<String> then(DocumentSnapshot document) {
if (!document.exists()) {
throw new DriverNotFoundException(); // <-- a custom exception of your choosing
}
return document.get("driver.tricyclenumber", String.class);
}
});
}
// to use:
getDriverTricycleNumber("someDriverId")
.addOnCompleteListener(new OnCompleteListener<Number>() {
#Override
public void onComplete(#NonNull Task<Number> task) {
if (task.isSuccessful()) {
String tricycleNumber = task.getResult();
Log.d(TAG, "Driver #" + driverId + "'s Tricycle Number is " + tricycleNumber);
} else {
Log.d(TAG, "Couldn't get tricycle number", task.getException());
}
}
});
Note: Optionally, you can simplify the above code using modern arrow notation and chaining:
public Task<String> getDriverTricycleNumber(String driverId) {
return FirebaseFirestore.getInstance()
.collection("Driver Locations")
.document(driverId)
.get()
.onSuccessTask(document -> {
if (!document.exists()) {
throw new DriverNotFoundException(); // <-- a custom exception of your choosing
}
return document.get("driver.tricyclenumber", String.class);
});
}
getDriverTricycleNumber("someDriverId")
.addOnSuccessListener(tricycleNumber -> {
Log.d(TAG, "Driver #" + driverId + "'s Tricycle Number is " + tricycleNumber);
})
.addOnFailureListener(exception -> {
Log.d(TAG, "Couldn't get tricycle number", exception);
});
If it's a map, then just use completely-standard dot notation:
const myFirstname = data.driver.firstname;
const myLastname = data.driver.lastname
etc etc etc
I'm new to firebase and recently started working on this. Every tutorial I saw, is about how to listen to data change in cloud firestore. What if I want to fetch teh data just once and don't wanna set anyy listener on them. Example scenario is, just fetching the data user last login time or profile data of any user
The documentation has a dedicated section about getting data a single time for a query. Just use the get() method on the query you build:
db.collection("cities")
.get()
.addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
#Override
public void onComplete(#NonNull Task<QuerySnapshot> task) {
if (task.isSuccessful()) {
for (QueryDocumentSnapshot document : task.getResult()) {
Log.d(TAG, document.getId() + " => " + document.getData());
}
} else {
Log.d(TAG, "Error getting documents: ", task.getException());
}
}
});
Or, for a single document:
DocumentReference docRef = db.collection("cities").document("SF");
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.d(TAG, "DocumentSnapshot data: " + document.getData());
} else {
Log.d(TAG, "No such document");
}
} else {
Log.d(TAG, "get failed with ", task.getException());
}
}
});
i have some problem understanding whats wrong with android code about calling function
as show below, I have 2 private void
cekSaved(place.getName());
addUserInfo(place.getName(),"");
i expect the android run ceksaved method first then addUserInfo but android running adduser first then run ceksaved function
i need help understanding this
code is :
private void cekSaved(String param1){
FirebaseFirestore db = FirebaseFirestore.getInstance();
DocumentReference newUserInfo = db.collection("trip").document();
String userID = FirebaseAuth.getInstance().getCurrentUser().getUid();
db.collection("trip")
.whereEqualTo("user_id", userID )
.whereEqualTo("city", param1)
.get()
.addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
#Override
public void onComplete(#NonNull Task<QuerySnapshot> task) {
if (task.isSuccessful())
{
for (QueryDocumentSnapshot document : task.getResult()) {
status = "ada";
Log.i(TAG, "onComplete: "+status);
}
}
else {
Log.d(TAG, "Error getting documents: ", task.getException());
}
}
});
}
private void addUserInfo(String city, String tittle){
FirebaseFirestore db = FirebaseFirestore.getInstance();
DocumentReference newUserInfo = db.collection("trip").document();
place_id = newUserInfo.getId();
Log.i(TAG, "addUserInfo: "+status);
if(!status.equals("ada")) {
String userID = FirebaseAuth.getInstance().getCurrentUser().getUid();
userTrip usertrip = new userTrip();
usertrip.setCity(city);
usertrip.setTittle("My Trip To " + city);
usertrip.setTrip_id(newUserInfo.getId());
usertrip.setUser_id(userID);
Log.i(TAG, "addUserInfo: trip baru telah di buat");
newUserInfo.set(usertrip).addOnCompleteListener(new OnCompleteListener<Void>() {
#Override
public void onComplete(#NonNull Task<Void> task) {
if (task.isSuccessful()) {
ToastMessage("OK");
} else {
ToastMessage("fail to register");
}
}
});
}else{ToastMessage("sudah ada");}
}
You're getting values from Firebase, it's an async call, so you don't really know which one will be completed first. You can see that either calls have an onCompleteListener, if you want to run addUserInfo after cekSaved you have to call addUserInfo inside onCompleteListener of cekSaved method.
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!
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 am new to Android and Firestore, I was able to retrieve data from Firestore without any problems.
But, I'm not sure how to use the reference.
Here's the data structure for person;
{ name=abc, gender=male, title=software engineer, company=abc, useraccount=com.google.firebase.firestore.DocumentReference#4878aac
}
So I get the useraccount-reference like this;
DocumentReference userAccountRef = doc.getDocumentReference("useraccount");
My question is how to retrieve information from user account by using this userAccountRef? I don't see any API to get a document by reference.
To get a document from a DocumentReference you call get() on it. From the Firebase documentation:
userAccountRef.get().addOnCompleteListener(new OnCompleteListener<DocumentSnapshot>() {
#Override
public void onComplete(#NonNull Task<DocumentSnapshot> task) {
if (task.isSuccessful()) {
DocumentSnapshot document = task.getResult();
if (document != null && document.exists()) {
Log.d(TAG, "DocumentSnapshot data: " + document.getData());
} else {
Log.d(TAG, "No such document");
}
} else {
Log.d(TAG, "get failed with ", task.getException());
}
}
});