I am using Realtime database feature of Firebase in my Application. Database looks like this:
The nodes 104, 1040... are having further elements/data. I'm fetching this data ordering it by value at a node 'updatedAt' available within every Integer parent as shown in the snap. I am using onChildAddedListener()
to fetch the last 20 elements. There are around 1050 nodes within 'results'.
The problem:
The fetching of data takes too much time, around 30 sec, even on high speed WiFi.
Does Firebase allows any callback to notify that data has been fetched? I want to append every custom object to the ArrayList
received from Firebase and pass it through intent to another activity. I am counting the objects received and when it becomes 20, I open up my Activity. Is that the only way to do so? If there is some better way out, do suggest.
Here is my code:
public class Splash extends AppCompatActivity{
private static final String TAG = "Splash Activity";
private ArrayList<News> NewsArrayList = new ArrayList<>();
private DatabaseReference newsReference;
private int position = 0;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splash);
newsReference = FirebaseDatabase.getInstance().getReference().child("results");
Query queryNewsReference = newsReference.orderByChild("updatedAt").limitToLast(20);
fetchNews(queryNewsReference);
}
private void fetchNews(Query queryNewsReference) {
queryNewsReference.addChildEventListener(new ChildEventListener() {
@Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
Log.d(TAG, "onChildAdded:" + dataSnapshot.getKey());
News newsObject = dataSnapshot.getValue(News.class);
Log.d(TAG,"News Title: "+ newsObject.getVideo_title());
NewsArrayList.add(newsObject);
position++;
if(position == 20){
startMainActivity();
}
}
@Override
public void onChildChanged(DataSnapshot dataSnapshot, String s){
Log.d(TAG, "onChildChanged() called on key: "+ dataSnapshot.getKey());
}
@Override
public void onChildRemoved(DataSnapshot dataSnapshot) {
Log.d(TAG, "onChildRemoved() called on key: "+ dataSnapshot.getKey());
}
@Override
public void onChildMoved(DataSnapshot dataSnapshot, String s) {
Log.d(TAG, "onChildMoved() called on key: "+ dataSnapshot.getKey());
}
@Override
public void onCancelled(DatabaseError databaseError) {
Log.e(TAG,"Database Error: "+databaseError.toString());
}
});
}
private void startMainActivity() {
Intent intent = new Intent(Splash.this,MainActivity.class);
intent.putExtra("NewsList",NewsArrayList);
Splash.this.startActivity(intent);
}
Try reconnecting to firebase before adding value listener:
private void fetchNews(Query queryNewsReference)
{
if (FirebaseDatabase.getInstance() != null)
{
FirebaseDatabase.getInstance().goOffline();
}
FirebaseDatabase.getInstance().goOnline();
queryNewsReference.addChildEventListener(new ChildEventListener() {
...
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With