3

I am working on Firebase to retreive data using addValueEventListener from Android SDK but i found sometime the response time take minimum 1 minute to get the result.

My Code :

Firebase firebase = new Firebase("https://example.firebaseio.com/");
firebase.child("XYZ").addValueEventListener(new ValueEventListener() {
                @Override
                public void onDataChange(DataSnapshot dataSnapshot) {

                    Log.d("DataFirebase","onDataChange : "+dataSnapshot);

                    Toast.makeText(getApplicationContext(),"onDataChange",Toast.LENGTH_SHORT).show();

                }

                @Override
                public void onCancelled(FirebaseError firebaseError) {

                    Log.d("DataFirebase","onCancelled : "+firebaseError);

                    Toast.makeText(getApplicationContext(),"onCancelled",Toast.LENGTH_SHORT).show();
                }
    });

This is my above code , please let me know , how can i get the result instantly from a key. Please suggest me some solution.

Anshuman Pattnaik
  • 883
  • 3
  • 16
  • 37
  • 2
    There is nothing in your code that would explain that slowness. These things are incredibly difficult to troubleshoot remotely. It comes down to a combination of the device you run this on, the network connection, the hops between you and the Firebase servers, the amount of data your program requests, etc. If you provide a [MCVE](http://stackoverflow.com/help/mcve) that runs slowly for you, we can run it for comparison. – Frank van Puffelen May 29 '16 at 14:33

1 Answers1

4

I too have a problem of slowness with firebase realtime db. Try calling, FirebaseDatabase.getInstance().setPersistenceEnabled(true); inside your Application class's "onCreate" method.

Be aware that this will save data on your device and the second call and above will be much faster but from cache and can be outdated, Read this for handling syncing data from server when needed immediately.

Shirane85
  • 2,255
  • 1
  • 26
  • 38