Is there any way to use firebase data as local cache in android to reduce loading time?

Viewed 174

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.

0 Answers
Related