Read and write data to Cloud Firestore in Android - android

Hi I want to read and write data using Firebase Firestore.
1- I `added Firebase to my project (using this instruction)
2- I added Firestore to my project (using this instruction)
I can write and then read data, but the read and write operation are locally and my Firestore database not changing.
I have added google-services.json to my app directory
I declared internet and network-state permission in manifest
My device is connected to internet
My Firestore database is in test mode (allow read, write)
This is my build.gradle dependency:
implementation platform('com.google.firebase:firebase-bom:29.1.0')
implementation 'com.google.firebase:firebase-analytics'
implementation 'com.google.firebase:firebase-firestore'
This is my code to add data to Firestore database:
private void writeData()
{
FirebaseFirestore db = FirebaseFirestore.getInstance();
Map<String, Object> user = new HashMap<>();
user.put("first", "Ada");
user.put("last", "Lovelace");
user.put("born", 1815);
db.collection("purchased")
.add(user)
.addOnSuccessListener(new OnSuccessListener<DocumentReference>() {
#Override
public void onSuccess(DocumentReference documentReference) {
Toast.makeText(MainActivity.this,"done",Toast.LENGTH_SHORT).show();
}
})
.addOnFailureListener(new OnFailureListener() {
#Override
public void onFailure(#NonNull Exception e) {
Toast.makeText(MainActivity.this,"failed",Toast.LENGTH_SHORT).show();
}
});
}
And this is my code to read data:
private void readData()
{
FirebaseFirestore db = FirebaseFirestore.getInstance();
db.collection("purchased")
.get()
.addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
#Override
public void onComplete(#NonNull Task<QuerySnapshot> task) {
if (task.isSuccessful()) {
for (QueryDocumentSnapshot document : task.getResult()) {
Toast.makeText(MainActivity.this,document.getId() + " => " + document.getData(),Toast.LENGTH_LONG).show();
}
} else {
Toast.makeText(MainActivity.this,"Error getting documents: "+ task.getException().getMessage().toString(),Toast.LENGTH_LONG).show();
}
}
});
}
While writing data, even callbacks are not calling (neither onSuccess nor onFailue) and no toast is displaying, but when I read data the toast display my data correctly, but everything is offline and no changes made to my Firestore database, what's the problem?

Related

How to get collection and subcollction from parent collection in cloud firestore android

I have user_info as parent collection. Under this parent collection it has single_list as child collection and some information. I want to get all values from parent collection. Please help me to find answer.
Thanks in advance
I think your use of terminology is a bit off. You have a collection of user_info documents, and each of those documents has a sub-collection named single_data.
The difference here is that there isn't a single single_data subcollection. There is one subcollection for each user_info document.
Since the subcollection has a static name, what you are looking to do is pretty straightforward using a collection group query.
firebase.firestore().collectionGroup('single_data')
.get()
.then((querySnapshot) => {
// Do something with the docs from across all the subcollections
})
FirebaseFirestore db = FirebaseFirestore.getInstance();
db.collection("users_info")
.get()
.addOnCompleteListener(new OnCompleteListener<DocumentSnapshot>() {
#Override
public void onComplete(#NonNull Task<DocumentSnapshot> task) {
if (task.isSuccessful()) {
for (DocumentSnapshot document : task.getResult()) {
//Here you can add all documents Id to your documentIdList to fetch all sigle_data at once.
Log.d(TAG, document.getId() + " => " + document.getData());
documentIdList.add(document.getId());
}
getAllSubCollSingleData(documentIdList);
} else {
Log.w(TAG, "Error getting documents.", task.getException());
}
}
});
public void getAllSubCollSingleData(List<Int> documentIdList){
for(int i=0;i<documentIdList.size();i++){
db.collection("users_info").document(documentIdList.get(i))(would be phone number)
.collection("single_data")
.get()
.addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
#Override
public void onComplete(#NonNull Task<QuerySnapshot> task) {
if (task.isSuccessful()) {
for (QueryDocumentSnapshot document : task.getResult()) {
//Here you can get all documents in sub collection single_data
}
} else {
Log.w(TAG, "Error getting documents.", task.getException());
}
}
});
}}

Check for document existence in Firestore

I'm trying to find if some item exists in my database.
If it does not exist, I had like to add it.
If it exists, I had like to show a message.
The code im using:
CollectionReference colRefMyBooks = db.collection( "Users" ).document( auth.getUid() ).collection( "MyBooks" );
Query queryMyBooks = colRefMyBooks.whereEqualTo("BookID", bookId);
queryMyBooks.get().addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
#Override
public void onComplete(#NonNull Task<QuerySnapshot> task) {
if (task.isSuccessful()) {
for (QueryDocumentSnapshot document : task.getResult()) {
Toast.makeText(BookDetailActivity.this, "Book already in my list", Toast.LENGTH_SHORT).show();
}
} else {
db.collection( "Users" ).document( auth.getUid() ).collection( "MyBooks" ).add( general_book );
}
}
});
This code works good as long as there is a collection "MyBooks". However, if there is no collection "Mybooks" I want it to consider it as the task is not successful and therefore to add the item.
What I do get is that it skips the whole onComplete and therefore does not add anything.
Does it mean that I have to check first if a collection exists and inside of it if document?
Thank you
A query that finds no documents will always be considered "successful", regardless of whether or not any collections exist. This behavior can't be changed.
What you'll have to do instead is check if there are no documents in the result set and decide what you want to do from there:
queryMyBooks.get().addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
#Override
public void onComplete(#NonNull Task<QuerySnapshot> task) {
if (task.isSuccessful()) {
QuerySnapshot snapshot = task.getResult();
if (snapshot.isEmpty()) {
db.collection( "Users" ).document( auth.getUid() ).collection( "MyBooks" ).add( general_book );
}
else {
for (QueryDocumentSnapshot document : snapshot) {
Toast.makeText(BookDetailActivity.this, "Book already in my list", Toast.LENGTH_SHORT).show();
}
}
}
}
});
Note that collections don't really "exist". There are no operations to add or delete collections. There are just operations to modify documents. A collection only appears in the console if it contains at least one document. If it contains no documents, it just disappears.

Ordering collectionGroup by serverTimestamp

I have made a database using Firestore.
The data base is as follows:
There is a main collection called NBDB, inside there are documents with uid for each user (for example OJQRFOyYd....). Inside each document there is another collection called MyBooks and inside this collection there are documents with uid that contain the BookID, DateCreated and Popularity.
By using the following code I have managed to obtain all of the BookIDs thank to collectionGroup query:
FirebaseFirestore db = FirebaseFirestore.getInstance();
db.collectionGroup( "MyBooks" ).get()
.addOnSuccessListener(new OnSuccessListener<QuerySnapshot>() {
#Override
public void onSuccess(QuerySnapshot queryDocumentSnapshots) {
for (QueryDocumentSnapshot document : queryDocumentSnapshots) {
Log.d(TAG,document.getId() + " => " + document.getData().get( "BookID"));
}
}
});
However, I had like the data to be ordered by the DateCreated field.
I thought first to order the database and then to use the function above, something like:
FirebaseFirestore db = FirebaseFirestore.getInstance();
db.collectionGroup( "MyBooks" ).orderBy("DateCreated").get()
.addOnSuccessListener(new OnSuccessListener<QuerySnapshot>() {
#Override
public void onSuccess(QuerySnapshot queryDocumentSnapshots) {
for (QueryDocumentSnapshot document : queryDocumentSnapshots) {
Log.d(TAG,document.getId() + " => " + document.getData().get( "BookID"));
}
}
});
However it is not working since my search is not inside the document of each book.
Is there a way to do it?
Thank you

how i can get list of name of folder in firebase storage android?

How can I get list of all folder file/,R52H50TN40F/,R58H42X2DCE/...
It's possible. Make sure you have this dependency in your build.gradle file:
dependencies {
...
implementation 'com.google.firebase:firebase-storage:19.1.1' // or later
}
This is how you can iterate over a list of directories and their entries:
listRef = FirebaseStorage.getInstance().getReference("/");
listRef.listAll()
.addOnSuccessListener(new OnSuccessListener<ListResult>() {
#Override
public void onSuccess(ListResult listResult) {
Log.d(TAG, listRef.getName());
for (StorageReference prefix : listResult.getPrefixes()) {
Log.d(TAG, prefix.getName());
// This will give you a folder name
// You may call listAll() recursively on them.
}
for (StorageReference item : listResult.getItems()) {
// All the items under listRef.
}
}
})
.addOnFailureListener(new OnFailureListener() {
#Override
public void onFailure(#NonNull Exception e) {
// Uh-oh, an error occurred!
}
});
It's not possible to get list of all folders with the firebase client api
But you can combine Firebase Functions and Cloud Storage bucket API.
Create a firebase function (documentation)
Use Firebase Bucket getFiles function

Querying Firestore for uid document after authentification => IllegalStateException

I am using both Firebase Authentification and Firestore for my Android app. What I am trying to do is the following:
the user signs in
if it's the first time the user signs in a document named by his uid is created
if the user has already signed in before (hence document named by uid already exists) then I load some further data.
Here's my logic to solve this:
get the FirebaseUser from FirebaseAuth instance
from the FirebaseUser I get the uid
build a DocumentReference with this uid
use get() query on the DocumentReference
if DocumentSnapshot is != null then user already exists in firestore
if DocumentSnapshot == null the user doesn't exist and I create it in firestore
I was testing the code below:
FirebaseUser user = mAuth.getCurrentUser();
if(user != null) {
// get uid from user
String uid = user.getUid();
// make a query to firestore db for uid
DocumentReference userDoc = db.collection("users").document(uid);
userDoc.get().addOnCompleteListener(new OnCompleteListener<DocumentSnapshot>() {
#Override
public void onComplete(#NonNull Task<DocumentSnapshot> task) {
if (task.isSuccessful()) {
DocumentSnapshot document = task.getResult();
if (document != null) {
Log.d(LOG_TAG, "DocumentSnapshot data: " + task.getResult().getData());
} else {
Log.d(LOG_TAG, "No such document");
}
} else {
Log.d(LOG_TAG, "get failed with ", task.getException());
}
}
});
}
When uid exists in firestore I get the log message with appropriate data but when it doesn't I get the following exception and I can't find a way to get to use DocumentSnapshot.exists():
java.lang.IllegalStateException: This document doesn't exist. Use DocumentSnapshot.exists() to check whether the document exists before accessing its fields.
Can anyone help me understand what I am doing wrong ?
Thanks a million ! :)
The object returned by get() is a DocumentSnapshot not the document itself. The DocumentSnapshot is never null. Use the exists() method to determine if the snapshot contains a document. If exists() is true, you can
safely use one of the getXXX() methods (in your case, getData() for a map) to obtain the value of the document.
#Override
public void onComplete(#NonNull Task<DocumentSnapshot> task) {
if (task.isSuccessful()) {
DocumentSnapshot snapshot = task.getResult();
if (snapshot.exists()) {
Log.d(LOG_TAG, "DocumentSnapshot data: " + snapshot.getData());
} else {
Log.d(LOG_TAG, "No such document");
}
} else {
Log.d(LOG_TAG, "get failed with ", task.getException());
}
}

Categories

Resources