3

I want to make an Android chat application. So I want to know how to get data from Firebase Firestore automatically, when new document create? Actually I do not wanna use add snapshot listener because of its give real-time data changes of a single document but want to find out real time updated Firebase Firestore document. Please suggest me.

Alex Mamo
  • 130,605
  • 17
  • 163
  • 193
Pradeep Sheoran
  • 493
  • 6
  • 15

3 Answers3

6

I don't know if you can do this without a snapshot listener. Check this code, if it is what you want.

private void addRealtimeUpdate() {
    DocumentReference contactListener=db.collection("PhoneBook").document("Contacts");
    contactListener.addSnapshotListener(new EventListener < DocumentSnapshot > () {
    @Override
    public void onEvent(DocumentSnapshot documentSnapshot, 
        FirebaseFirestoreException e) {
         if (e != null) {
            Log.d("ERROR", e.getMessage());
            return;
         }
         if (documentSnapshot != null && documentSnapshot.exists()) {
            Toast.makeText(MainActivity.this, "Current data:" + 
          documentSnapshot.getData(), Toast.LENGTH_SHORT).show();
        }
     }
    });
  }
Thelouras
  • 852
  • 1
  • 10
  • 30
4

To solve this, I recommend you to use CollectionReference's get() method. This is the correspondent addListenerForSingleValueEvent() method from Firebase real-time database.

Executes the query and returns the results as a QuerySnapshot.

If you want to use Firebase-UI library, this is a recommended way in which you can retrieve data from a Cloud Firestore database and display it in a RecyclerView using FirestoreRecyclerAdapter.

Alex Mamo
  • 130,605
  • 17
  • 163
  • 193
  • I don't understand how you could accept an answer that provides you a solution using `addSnapshotListener` since you explicitly requested in your question that: `I do not wanna use add snapshot listener`?! – Alex Mamo Aug 23 '18 at 11:48
  • addListenerForSingleValueEvent() gives the documents value every time when new documents create ? – Pradeep Sheoran Aug 24 '18 at 04:49
  • Yes, that's right but this is happening in Firebase real-time database. The correspondent in Firestore is the `get()` method, right? – Alex Mamo Aug 24 '18 at 07:42
  • get() method only works when we call it. without call a method again and again how it is possible. Suppose new document comes in then how automatic data fetch from firestore – Pradeep Sheoran Aug 24 '18 at 09:47
  • If you want data in real-time, you should add a listener but you said you don't want. If you want to get the data only once, then just a `get()` call. If you want more than that, you can use Firebase-UI like is explained in my answer from this **[post](https://stackoverflow.com/questions/49277797/how-to-display-data-from-firestore-in-a-recyclerview-with-android/49277842)** where I have explained how you can retrieve data from a Cloud Firestore database and display it in a `RecyclerView` using `FirebaseRecyclerAdapter`. In this ase you'll be able to receive every new document that you added. – Alex Mamo Aug 24 '18 at 09:54
1

I don't know if you can do it with firestore but with realtime database you can use the .on(). If the rest of your app is using firestore, each project can use both a cloud firestore and a realtime database. The docs are really simple.

Tablesalt
  • 11
  • 3