I'm trying to get a user's profile from a Firebase DB. Then using the user's information I want to set TextViews in my Fragment's layout to reflect the user's individual stats.
The problem is that the rootViw is being returned prior to having recieved the user's profile. And so I get a null object reference error.
My understanding of the fragment's life cycle is that onCreate() is created first and so I tried placing the DB code there but I get the same problem. I then figured that if accessing the DB is slower than my onCreateView() I'll place a Thread.sleep() timer to wait for the DB call to complete and then perform the rest of my code. Which I know is a stupid solution but just wanted to test my theory; that also failed so obviously my understanding is wrong.
Where should I place my DB call so that it completes prior to returning my rootView? Why does placing the DB listener in OnCreate() not work and why does the Thread.sleep() delay not work?
Leaderboard Fragment
public class Leaderboard extends Fragment{
private FirebaseAuth mFirebaseAuth;
private DatabaseReference mUserDatabaseReference;
private User user;
private TextView scoreView;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_leaderboard,
container, false);
scoreView = rootView.findViewById(R.id.leaderboard_score);
mFirebaseAuth = FirebaseAuth.getInstance();
final String userUID = mFirebaseAuth.getCurrentUser().getUid();
mUserDatabaseReference =
FirebaseDatabase.getInstance().getReference("users");
mUserDatabaseReference.addListenerForSingleValueEvent(new
ValueEventListener() {
#Override
public void onDataChange(DataSnapshot snapshot) {
if (snapshot.hasChildren()) {
for (DataSnapshot messageSnap: snapshot.getChildren()) {
if(messageSnapshot.getKey().equals(userUID)) {
user = messageSnapshot.getValue(User.class);
}}}}
#Override
public void onCancelled(DatabaseError databaseError) {}
});
//Causes error because user==null
scoreView.setText("Score: " + user.getScore());
return rootView;
}
}
All Firebase APIs are asynchronous. You should expect that listeners may be called after any amount of time, based on the quality of the hardware and its network connection. Don't ever use Thread.sleep() to try to control the timing of things - that is an anti-pattern.
My suggestion to you is to inflate a "loading" screen in onCreateView() to display immediately, so the user doesn't have to look at a blank screen when your fragment starts. Then, when your listener is called with the data you want to display, add or update other views as needed.
Why not set the score after you get the DB result ?
mUserDatabaseReference.addListenerForSingleValueEvent(new
ValueEventListener() {
#Override
public void onDataChange(DataSnapshot snapshot) {
if (snapshot.hasChildren()) {
for (DataSnapshot messageSnap: snapshot.getChildren()) {
if(messageSnapshot.getKey().equals(userUID)) {
user = messageSnapshot.getValue(User.class);
}
}
scoreView.setText("Score: " + user.getScore());
}}
#Override
public void onCancelled(DatabaseError databaseError) {}
});
Related
I am working on an Android app with 2 types of users (doctors and patients), and I want each type to have their own UI. For eg, doctors must see ' Add days off' and patients must see ' Book appointment' . Somehow I don't get anywhere with whatever I try.
I also use Firebase Auth and Realtime Database which makes the user type retrieval kinda tricky. So far I've tried a lot of Async classes, methods, variables, shared preferences, retrieving data while on launcher splash screen.
The best I got is getting the user to login, it shows the good layout, then I start the app again and it shows the wrong layout. Somehow I noticed it just works on the second run, but not always so the behaviour is unpredictable to me at least. But at least the user type from the database is retrieved.
I have a class that extends Application, which checks if there's an user authenticated and then redirects the user to either LoginActivity, or MainMenuActivity.
I have created a method that retrieves the Firebase Auth user data from Realtime Database, looping through both Doctors and Patients 'children' until it finds the current user email and gets its type. Since Realtime Database is asynchronous, the methos gets an interface as an argument, and after the loop, the I call the interface's method, which sets a static boolean variable (isUserDoctor).
Before setting the content view (with 2 possible layouts), I call the function described before and it works the way I first mentioned, which is not good.
The method that retrives data
public void getUserType(final DataStatus dataStatus) {
currentUser = FirebaseAuth.getInstance().getCurrentUser();
currentUserEmail = currentUser.getEmail();
databaseReference = FirebaseDatabase.getInstance().getReference("Users");
databaseReference.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot dataSnapshot) {
currentUserType.clear();
FirebaseManager.isUserDoctor = false;
DataSnapshot allDoctors = dataSnapshot.child("Doctors");
DataSnapshot allPatients = dataSnapshot.child("Patients");
for (DataSnapshot ds : allDoctors.getChildren()) {
if (currentUserEmail.equals(Utils.decodeUserEmail(ds.getKey()))) {
currentUserType.add(ds.child("userType").getValue().toString());
} else {
for (DataSnapshot dsPacient : allPatients.getChildren()) {
if (currentUserEmail.equals(Utils.decodeUserEmail(dsPacient.getKey()))) {
currentUserType.add(dsPacient.child("userType").getValue().toString());
}
}
}
}
dataStatus.DataIsLoaded(currentUserType.get(0).toString());
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {
}
});
The interface
public interface DataStatus {
void DataIsLoaded(String userType);
}
The method's call in Main Menu
FirebaseManager.getInstance().getUserType(new DataStatus() {
#Override
public void DataIsLoaded(String userType) {
if ("doctor".equals(userType))
FirebaseManager.isUserDoctor = true;
else
FirebaseManager.isUserDoctor = false;
}
});
if (FirebaseManager.isUserDoctor)
setContentView(R.layout.activity_main_menu_doctor);
else
setContentView(R.layout.activity_main_menu);
So if anyone has any ideas about how to show the proper layout and allow functions based on user role/type please share. What I basically need is to retrieve the userType from the current email just in time to set a variable needed throughout the whole app in order to hide/show certain views.
I am trying to retrieve data from Firebase realtime-database and put it on a CardView inside a RecyclerView inside a Fragment.
But the Fragment shown is blank white with no error. I retrieve the data inside OnCreate method and add it into a List.
While debugging the application, found out that even after assigning the retrieved data inside the onCreate method, the list is still NULL inside the onCreateView method.
Fragment Dashboard List Class:
public class fragment_dashboard_list extends Fragment {
List<ibu> ibu_ibu;
FirebaseDatabase database;
DatabaseReference myRef ;
String a;
public fragment_dashboard_list() {}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ibu_ibu = new ArrayList<>();
database = FirebaseDatabase.getInstance();
myRef = database.getReference("Guardian");
myRef.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
// This method is called once with the initial value and again
// whenever data at this location is updated.
for(DataSnapshot dataSnapshot1 : dataSnapshot.getChildren()){
ibu value = dataSnapshot1.getValue(ibu.class);
ibu ibu_val = new ibu();
String alamat = value.getAlamat();
String foto = value.getFoto();
String hp = value.getHp();
String ktp = value.getKtp();
String nama = value.getNama();
String privilege = value.getPrivilege();
String ttl = value.getTtl();
ibu_val.setAlamat(alamat);
ibu_val.setFoto(foto);
ibu_val.setHp(hp);
ibu_val.setKtp(ktp);
ibu_val.setNama(nama);
ibu_val.setPrivilege(privilege);
ibu_val.setTtl(ttl);
// Here the List ibu_ibu is not NULL
ibu_ibu.add(ibu_val);
}
}
#Override
public void onCancelled(DatabaseError error) {
// Failed to read value
Log.w("Hello", "Failed to read value.", error.toException());
}
});
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_dashboard_list, container, false);
RecyclerView myrv = (RecyclerView) view.findViewById(R.id.dashboard_recycler_view);
//Here the List ibu_ibu is null
adapter_list_ibu myAdapter = new adapter_list_ibu(ibu_ibu);
LinearLayoutManager LinearLayoutManager = new LinearLayoutManager(getContext());
myrv.setLayoutManager(LinearLayoutManager);
myrv.setAdapter(myAdapter);
return view;
}
}
I expected the List to be not NULL inside OnCreateView so the Fragment wont Blank
Firebase APIs are asynchronous, which means that onDataChange() method returns immediately after it's invoked, and the callback will be called some time later. There are no guarantees about how long it will take. So it may take from a few hundred milliseconds to a few seconds before that data is available.
Because that method returns immediately, your ibu_val list that you're trying to use it outside the onDataChange() method, will not have been populated from the callback yet and that's why is always empty.
Basically, you're trying to use a value of variable synchronously from an API that's asynchronous. That's not a good idea, you should handle the APIs asynchronously as intended.
A quick solve for this problem would be to notify the adapter once you got all elements from the database using:
myrv.notifyDatasetChanged();
So add this line of code right after where the for loop ends.
If you intent to use that list outside the callback, I recommend you see the last part of my anwser from this post in which I have explained how it can be done using a custom callback. You can also take a look at this video for a better understanding.
If I set scoresRef.keepSynced(false) and use Disk Persistence, FirebaseDatabase.getInstance().setPersistenceEnabled(true); to store the data locally, will it lower down the number of "Simultaneous connections" to firebase DB as there will be no active listeners(or it isn't?) ? what may be the consequences?
Codes:
I have a custom adapter "firebaseadapter" and a class "firebasestore" with getter/setter methods. Since "calls to setPersistenceEnabled must be made before any other usage of firebase Database instance", I have made a different class extending Application(or using it in main activity class with static {} is better?).
Utility.calculateNoOfColumns is calculating the number grids to be shown based on screen size.
Moreover, Will the data get updated in client side in real time if I make any changes in firebase DB if the set scoresRef.keepSynced(false)?
public class ThreeFragment extends Fragment {
View viewThree;
ArrayList<firebasestore> list;
DatabaseReference mdatabase;
GridLayoutManager gridLayoutManager;
private firebaseAdapter firebaseAdapter1;
FirebaseDatabase database;
public ThreeFragment() {
// Required empty public constructor
}
#Override
public void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
FirebaseApp.initializeApp(getContext());
database= FirebaseDatabase.getInstance();
mdatabase=database.getReference().child("DBName");
mdatabase.keepSynced(false);
list = new ArrayList<>();
loadStoreDetails();
}
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
viewThree = inflater.inflate(R.layout.fragment_three, container, false);
int mNoOfColumns = Utility.calculateNoOfColumns(getContext());
RecyclerView firebaseRecyclerView = (RecyclerView)
viewThree.findViewById(R.id.recyclerview_threeFragment1);
firebaseRecyclerView.setHasFixedSize(true);
firebaseAdapter1 = new firebaseAdapter(getContext(), list);
firebaseRecyclerView.setLayoutManager(gridLayoutManager);
firebaseRecyclerView.setAdapter(firebaseAdapter1);
return viewThree;
}
// get data from firebase DB
private void loadStoreDetails() {
ValueEventListener valueEventListener = new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
list.clear(); // CLAER DATA BEFORE CHANGING. IF NOT DONE, IT WILL SHOW DUPLICATE DATA
for(DataSnapshot ds : dataSnapshot.getChildren()) {
list.add(ds.getValue(firebasestore.class));
}
firebaseAdapter1.notifyDataSetChanged(); // NOTIFY ADAPTER TO SHOW DATA IN VIEW WITHOUT RELOAD
}
#Override
public void onCancelled(DatabaseError databaseError) {
Log.w("LogFragment", "loadLog:onCancelled", databaseError.toException());
}
};
mdatabase.limitToLast(20).addValueEventListener(valueEventListener);
}
}
If there are no active listeners for a minute, the Firebase client will indeed close its connection to the server.
In your code you call loadStoreDetails attaches a listener with addValueEventListener from onCreate. Since you never remove that listener, it will stay active permanently from the moment ThreeFragment is created until the program exits.
To prevent this, and ensure the data is only synchronized (and the connection kept open) while the user has the fragment open, detach the listener in onDestroyView or onDestroy of the fragment.
For that, add a member field to the fragment:
ValueEventListener mFragmentListener;
Then keep a reference to the listener when you attach it:
mFragmentListener = mdatabase.limitToLast(20).addValueEventListener(valueEventListener);
And finally remove the listener when the fragment is destroyed:
#Override
public void onDestroyView() {
mdatabase.limitToLast(20).removeEventListener(mFragmentListener);
}
On a separate note: the call to mdatabase.keepSynced(false); is not needed in your code, as that is the default behavior already.
I've just started with Firebase and I am not entirely sure, that my approach is the right one, so maybe someone could help me out there.
public class CharakterFragment extends Fragment {
String TAG = this.getClass().getSimpleName();
FirebaseDatabase database = FirebaseDatabase.getInstance();
DatabaseReference testRef = database.getReference("charakter/1/Name");
String wert;
public CharakterFragment() {
// Required empty public constructor
}
#Override
public void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
testRef.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
// This method is called once with the initial value and again
// whenever data at this location is updated.
wert = dataSnapshot.getValue(String.class);
Log.d(TAG, "Value is: " + wert);
}
#Override
public void onCancelled(DatabaseError error) {
// Failed to read value
Log.w(TAG, "Failed to read value.", error.toException());
}
});
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_charakter, container, false);
Log.d(TAG, "Value in view: " + wert);
TextView textNameValue = (TextView) view.findViewById(R.id.textNameValue);
textNameValue.setText(wert);
return view;
}
}
The problem here is, that as soon as I load the Fragment ( I am using a Navigation Drawer Activity ), it takes about a second until the textView gets its update and until then it shows the predefined value.
Is there anything I could do to preload it or speed the process up? Also, is this the right approach to handle retrieving the data?
Edited the code according to the first answer.
You can call the firebase addValuEventListener() method from onCreate(), so you'll get all the data from panel before showing th view. Then just set the data in onCreateView()
What is the purpose of this app? By that I mean, is it just an application with users and profiles, like social media or is it a game (It might be relevant to your question)
Usually, with apps that gather data from an online API like firebase, latest data is cached to the device using whatever file format you choose. (THIS might help you decide). Once that data is saved, you load it into your layout's views when the app loads, at the same time calling your API from firebase. This way your layout won't have their predefined values showing while the fetched results are loading.
Think of an app like Facebook or Twitter. Your news feed has the last few status updates you saw when you last exited the app, and after a few seconds, the newly fetched newsfeed automatically refreshes, without you having to pull to refresh.
Yet now i am getting the all data from the FireBase at one time.What i want to do that getting data in LIMITS like 15 records at a time. Like in first time user get the 15 records from the Firebase and when user load more data at the bottom/TOP of the screen than 15 more records should come from Firebase and added to the bottom/TOP of the list.
I have implemented the logic to get the 15 records at a top OR bottom of the database from Firebase like below:-
public class ChatActivity extends AppCompatActivity implements FirebaseAuth.AuthStateListener {
private FirebaseAuth mAuth;
private DatabaseReference mChatRef;
private Query postQuery;
private String newestPostId;
private String oldestPostId;
private int startAt = 0;
private SwipeRefreshLayout swipeRefreshLayout;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_chat);
mAuth = FirebaseAuth.getInstance();
mAuth.addAuthStateListener(this);
mChatRef = FirebaseDatabase.getInstance().getReference();
mChatRef = mChatRef.child("chats");
/////GETTING THE VIEW ID OF SWIPE LAYOUT
swipeRefreshLayout = (SwipeRefreshLayout) findViewById(R.id.swipeRefreshLayout);
/////GETTING FIRST 10 RECORDS FROM THE FIREBASE HERE
mChatRef.limitToFirst(10).addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot child : dataSnapshot.getChildren()) {
oldestPostId = child.getKey();
System.out.println("here si the data==>>" + child.getKey());
}
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
//////FOR THE PULL TO REFRESH CODE IS HERE
swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
#Override
public void onRefresh() {
// Refresh items
System.out.println("Here==>>> "+oldestPostId);
///HERE "oldestPostId" IS THE KEY WHICH I GET THE LAST RECORDS FROM THE FIREBASE
mChatRef.startAt(oldestPostId).addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot child : dataSnapshot.getChildren()) {
System.out.println("here AFTER data added==>>" + child.getKey());
}
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
});
}
I have searched here on SO for it , but did not get the expected result, below link which i have searched for it
1. First Link
2. Second Link
3. Third Link
4. Forth Link
Please look at my firebase data structure in image.
I have implemented the logic for the getting 15 OR 10 records at first time and it works..and also implemented the logic for loading more records in limits but not getting proper solution (NOT WORKING) , please help and let me know where am i doing wrong..Thanks :)
EDIT SOLUTION
:- I have implemented the load more or pull to refresh functionality on this link:- Firebase infinite scroll list view Load 10 items on Scrolling
You are missing orderByKey(). For any filtering queries you must use the ordering functions. Refer to the documentation
In your onRefresh method you need to set the limit:
public void onRefresh() {
// Refresh items
///HERE "oldestPostId" IS THE KEY WHICH I GET THE LAST RECORDS FROM THE FIREBASE
mChatRef.orderByKey().startAt(oldestPostId).limitToFirst(10).addListenerForSingleValueEvent(new ValueEventListener() {
.....
So the data you retrieve is the only 10 new records after you got your first 10 records.
Make sure to save the oldest key of the newly retrieved data so that on next refresh new data from this key is only retrieved.
Suggestion: Instead of adding a child value listener to find the last key, you can just use the value listener and get the last data snapshot with the size to get the last record's key.
restructure your database, set a new child id which is incremental like 0,1,2,3... etc
"chats": {
"-KZLKDF": {
"id": 0,
"message": "Hi",
"name":"username"
},
then make a method for query
public void loadFromFirebase(int startValue,int endValue){
mChatRef.orderByChild(id).startAt(startValue).endAt(endValue).addListenerForSingleValueEvent(this);
}
make sure you have implemented addListenerForSingleValueEvent then do adapter related task.
Initially call from onCreate method:
loadFromFirebase(0,10);
NB: loading new content in the list you have to be aware with adapter.