Lorek Bryanson Lorek Bryanson - 3 months ago 53
Android Question

Android - Persist data retrieved from Firebase

I have an activity and a model called

CourseDetails
.

String getData;
DatabaseReference mRef = FirebaseDatabase.getInstance().getReference().child("courses").child("Business");
mRef.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
CourseDetails c = dataSnapshot.getValue(CourseDetails.class);
getData = c.getCourseName();
}

@Override
public void onCancelled(DatabaseError databaseError) {

}
});

textview1.setText(getData);


Using above code throws
NullPointerException
at last line above. But if I put
textview1.setText(getData)
into the
ValueEventListener
, under
getData = c.getCourseName()
, the data can be displayed correctly.

Methods I found working are using
SharedPreferences
or setting data from a method such as
public void display(String data) { textview1.setText(data); }
. But what are the other ways to keep the retrieved data even if the data is outside
ValueEventListener
?

For instance I want to persist the data added into an
ArrayList
.

ArrayList<String> listData;
DatabaseReference mRef = FirebaseDatabase.getInstance().getReference().child("courses").child("Business");
mRef.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot ds : dataSnapshot.getChildren()) {
CourseDetails c = dataSnapshot.getValue(CourseDetails.class);
String code = c.getCourseCode();
String name = c.getCourseName();
String CodeName = code + " " + name;

listData.add(CodeName);
}
}

@Override
public void onCancelled(DatabaseError databaseError) {

}
});

// data in ArrayList should be able to display here
StringBuilder builder = new StringBuilder();
for (String s : listData) {
builder.append(s + "\n");
}

textview1.setText(builder.toString());


How to achieve this kind of persistence?

Answer

As per my understanding, Firebase will notify all it's data listener attached to specific references (database references wherever the addValueEventListener is added) when those specific data gets modified. That is when onDataChange will be called, when there is modification of the data at those database references, (besides modification the method will always be called first time).

And this happens asynchronously, so in the first case where null occurs because we don't know whether data is retreived from Firebase and as far as I know, Android's main thread cannot be put on hold or pause until we retreive the data that's why we use Asynchronous tasks in Android. So, I think the best way to do specific updates or task on data change is within onDataChange method. So, like you stated it could be done by making those changes within onDataChange itself or by calling some other method from onDataChange.

Or, if you are using adapter then, notifying adapter about the change within onDataChange. Also, you can take a look at other choice i.e. FirebaseRecyclerAdapter then, it will handle the update automatically without any extra effort.