'Read and write data to Cloud Firestore in 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?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source