Reputation: 125
I'm making an app and retrieving data for profile like name, age, username, dob, etc., from a firebase database. But, when I open the app and go to the profile page, it takes about 2 to 3 seconds to refresh the text there and then display the data. I want to make that process faster. Is there anyway to do that? And is it possible to store that data within local storage of the app and display it and update that stored data later using internet and data from firebase database? The same principal I wanted to apply to images which load from firebase storage. Those take much longer than the data. Approximately 4 to 5 seconds.
This is what I'm currently using:
mDatabase = FirebaseDatabase.getInstance().getReference().child(current_user);
mDatabase.keepSynced(true);
mDatabase.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
String first = dataSnapshot.child("firstname").getValue().toString();
String last = dataSnapshot.child("lastname").getValue().toString();
String friend = dataSnapshot.child("friends").getValue().toString();
String post = dataSnapshot.child("posts").getValue().toString();
String user = dataSnapshot.child("username").getValue().toString();
String db = dataSnapshot.child("age").getValue().toString();
String ag = dataSnapshot.child("dob").getValue().toString();
String bo = dataSnapshot.child("bio").getValue().toString();
String rela = dataSnapshot.child("relationshipstatus").getValue().toString();
firstname.setText(first);
lasttname.setText(last);
username.setText(user);
friends.setText("Friends:"+friend);
posts.setText("Posts:"+post);
dob.setText("DOB:"+db);
age.setText("Age:"+ag);
bio.setText(bo);
relation.setText(rela);
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
Upvotes: 3
Views: 1399
Reputation: 576
Whenever you're storing user profile in firebase first store it in local, use shared preference or room whichever suits you more, and after that whenever you do any update, first do it in local data and then sync it to firebase storage, that way the data will always be their with you until user clears it. For this always make a check whether local data exists, if it doesn't get data from firebase storage then save it in local. Same way use caching for storing images in local, use glide or fresco for imageloading, then add simple code for caching, so until image url changes or local data is deleted you'll always get image very fast.
Upvotes: 4