0

I made an application that users can earn points by taking quizzes. I have a leader board too.

my primary database is cloud firestore. but I need the leaderboard to be more real-time like it needs to update every time when a user earns points without refreshing or closing the fragment.

So I need to connect firebase firestore to real-time databases, (if I change the firestore data(like coins for the specified user or any), it needs to change the real-time data too)

I made codes but it didn't work well. I have attached the code here.

 private void LoadFirestore() {
    firebaseFirestore.collection("Users")
            .document(FirebaseAuth.getInstance().getUid())
            .get().addOnSuccessListener(new OnSuccessListener<DocumentSnapshot>() {
        @Override
        public void onSuccess(DocumentSnapshot documentSnapshot) {

            user = documentSnapshot.toObject(User.class); 
            totalCoins.setText(String.valueOf(user.getCoins()));

        }
    });
}


private void uploadToRealtime() {

    HashMap<String, Object> map = new HashMap<>();
    map.put("coins", totalCoins);
    firebaseDatabase.getReference().child("Users").child(FirebaseAuth.getInstance().getUid())

            .updateChildren(map);


            }
}
Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
Angel
  • 115
  • 1
  • 6
  • 1
    Firestore supports [realtime listeners too](https://firebase.google.com/docs/firestore/query-data/listen), so did you consider https://stackoverflow.com/questions/46720997/leaderboard-ranking-with-firebase? – Frank van Puffelen Jun 23 '21 at 18:23
  • @FrankvanPuffelen but if I added some bonus point to a specified user in firestore, it won't appear in the real-time database. – Angel Jun 23 '21 at 18:30
  • Why would you use both databases? – Alex Mamo Jun 24 '21 at 08:34
  • @AlexMamo because real-time DB will get the data more real-time, its too hard to find some datas. – Angel Jun 24 '21 at 11:59
  • @AlexMamo because real-time DB will get the data more real-time, it's too hard to find some data. and firestore is more acceptable for finding specific data, can add to deferent categories, collection, document, fields. but it is not real time. – Angel Jun 24 '21 at 11:59
  • The Realtime Database doesn't get the data more real-time than Cloud Firestore. You can confidently use Cloud Firestore ;) – Alex Mamo Jun 24 '21 at 12:00
  • @AlexMamo is it ?. but when a user earn points it will not add to the points screen until they refreshed or closed and reopen the fragment – Angel Jun 24 '21 at 12:02
  • Yes, it does. As FrankvanPuffelen already mentioned in his comment, use [real-time listeners](https://firebase.google.com/docs/firestore/query-data/listen). – Alex Mamo Jun 24 '21 at 14:17

1 Answers1

1

You can use a onSnapshotListener to get the fata directly from firestore in realtime. Here is a basic example for that:

final DocumentReference docRef = db.collection("cities").document("SF");
docRef.addSnapshotListener(new EventListener<DocumentSnapshot>() {
    @Override
    public void onEvent(@Nullable DocumentSnapshot snapshot,
                        @Nullable FirebaseFirestoreException e) {
        if (e != null) {
            Log.w(TAG, "Listen failed.", e);
            return;
        }

        if (snapshot != null && snapshot.exists()) {
            Log.d(TAG, "Current data: " + snapshot.getData());
        } else {
            Log.d(TAG, "Current data: null");
        }
    }
});

You can check more about it here.

Creating a sync to the RealtimeDatabase just for the realtime feature wouldn't make any sense here and would give you more costs in your Firebase project.

Tarik Huber
  • 7,061
  • 2
  • 12
  • 18