2

I am making one chat application and want to fetch all users from the real-time firebase database on the chat screen using the custom ArrayList adapter. Unfortunately, whenever I open that screen it loads all the data from firebase; So, it takes too time to display all users. I wondered if I can store users and their chat messages offline on App local storage and use them by synchronizing with an online database.

I have tried FirebaseDatabase.getInstance().setPersistenceEnabled(true) and usersRef.keepSynced(true) in following code but it doesn't work for me.

FirebaseDatabase.getInstance().setPersistenceEnabled(true);
DatabaseReference usersRef = FirebaseDatabase.getInstance().getReference("users");
usersRef.keepSynced(true);
usersRef.addValueEventListener(new ValueEventListener() {
                @Override
                public void onDataChange(@NonNull DataSnapshot snapshot) {
                    myFriendUnames.clear();
                    for (DataSnapshot snap : snapshot.getChildren()) {
                        myFriendUnames.add(snap.getKey().toString());
                        Log.d(TAG, "Key +++++++ " + snap.getKey() + "++++++++++");

                        //here I am adding items to the list adapter
                    }
});

//set the adapter
//On click listener of adapter

I have tried to see the following resources but was not able to get needed information.

https://firebase.google.com/docs/database/android/offline-capabilities
Android Firebase - Local Cached Data
Firebase offline capabilities as cache

Any help would be much appreciated. Thanks in Advance.

Karm Patel
  • 174
  • 1
  • 7
  • Regardless of why Firebase caching does not work for you, you may save all the user info in your own local file, and sync it with the database in on change listener. – Rediska Sep 28 '21 at 18:49
  • 1
    "it takes too time to display all users" What is too much here? How did you measure this? Can you add some logging to the code to show the problem? "it doesn't work for me" What does that mean? If you made the changes I asked for, how is the logging output changed after you enable caching? – Frank van Puffelen Sep 28 '21 at 23:14
  • @Rediska Thanks for the reply, Can you please give me some resources to do the same? – Karm Patel Sep 29 '21 at 06:10
  • 1
    @Karm Patel I mean, you may retrieve the data the usual way using addValueEventListener(), and save it into a local file. When the data in the database change, the ValueEventListener will update the local file. In your UI code, you always use the local file as the data source. – Rediska Sep 29 '21 at 19:39

0 Answers0