Android: Firebase takes too long to retrieve data

I am using the Realtime Firebase database function in my application. The database looks like this: database binding

Nodes 104, 1040 ... have additional elements / data. I get this data by sorting it by value in the node 'updatedAt', available for each Integer parent, as shown in the snap-in. I am using onChildAddedListener() to retrieve the last 20 items. In the "results" there are about 1050 nodes.

Problem:

  • Data extraction takes too much time, about 30 seconds, even on high-speed WiFi.

  • Does Firebase provide any callback to notify that data has been received? I want to add each custom object to an ArrayList received from Firebase, and pass it using intent to another activity. I count the items received, and when I get 20, I discover my activity. Is this the only way to do this? If there is any better way, 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); } 
+5
source share
1 answer

Try connecting to firebase before adding a value listener:

 private void fetchNews(Query queryNewsReference) { if (FirebaseDatabase.getInstance() != null) { FirebaseDatabase.getInstance().goOffline(); } FirebaseDatabase.getInstance().goOnline(); queryNewsReference.addChildEventListener(new ChildEventListener() { ... } 
0
source

All Articles