Reputation: 130
Background:
I am working on a book reading application where users can create their profile and then can login for reading books that are uploaded by certain readers. I am using firebase database, where all the information of users ( i.e. Profile picture, name etc.) as well as all the books (that are in pdf format are stored). I am using a recycler view for displaying the cover pages of books, and when user clicks it, the corresponding pdf file for the book that is fetched from firebase database open's in a new activity.
Problem:
Now the problem that I am facing is that every time the application is restarted, it fetches all the books from database that takes some time. I want that all of the data i.e. books and user info that is once fetched from firebase database, should be stored in mobiles memory so that when user restarts the application, every thing in recycler view should be retrieved from mobile. Like in the WhatsApp, whenever we restart it , it displays everything within no time from mobile storage and updates the recycler view. How would I achieve that ?
Upvotes: 0
Views: 609
Reputation: 1
FirebaseFirestoreSettings settings = new FirebaseFirestoreSettings.Builder()
.setPersistenceEnabled(true) //enable offline persistence.
.build();
db.setFirestoreSettings(settings);
Upvotes: 0
Reputation: 130
Solution:
I've made a new class and overridden onCreate()
method as follow:
public class NetworkSecurity extends Application {
@Override
public void onCreate() {
super.onCreate();
FirebaseDatabase.getInstance().setPersistenceEnabled(true);
}
}
After that I set keepSynced
attribute to true:
final DatabaseReference ref = FirebaseDatabase.getInstance().getReference("Books");
ref.keepSynced(true);
and finally added the name of class in manifest.xml as follow:
android:name=".NetworkSecurity"
and finally it worked.
@Citut Your answer was helpful. Thanks.
Upvotes: 1
Reputation: 907
I think you want to enable persistence, I believe firebase supports this. You will also want to configure the cache as well.
Enable persistence:
FirebaseFirestoreSettings settings = new FirebaseFirestoreSettings.Builder()
.setPersistenceEnabled(true) //enable offline persistence.
.build();
db.setFirestoreSettings(settings);
Configure caching:
// The default cache size threshold is 100 MB. Configure "setCacheSizeBytes"
// for a different threshold (minimum 1 MB) or set to "CACHE_SIZE_UNLIMITED"
// to disable clean-up.
FirebaseFirestoreSettings settings = new FirebaseFirestoreSettings.Builder()
.setCacheSizeBytes(FirebaseFirestoreSettings.CACHE_SIZE_UNLIMITED)
.build();
db.setFirestoreSettings(settings);
Check the docs for Android here: https://firebase.google.com/docs/firestore/manage-data/enable-offline#java_1
Upvotes: 1