Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Firebase gets stuck on no INTERNET connection

In my app, I am using Firebase for database. Now when I am listening for any change in child nodes of a particular location, and if there is no internet connection, no callback gets fired. The firebase call just gets stuck.

I am using this,

FirebaseDatabase firebaseDatabase = FirebaseDatabase.getInstance();
firebaseDatabase.setPersistenceEnabled(true);

So, persistence is enabled. The problem occurs only one one scenario. When there is no data on the local persistence of Firebase and the internet connection is also not available.

I am using this,

addListenerForSingleValueEvent(new ValueEventListener() {
                        @Override
                        public void onDataChange(DataSnapshot dataSnapshot) {

                        }

                        @Override
                        public void onCancelled(DatabaseError databaseError) {
                            Timber.d("Cancelled");
                        }
                    });

I am not getting any of the two callbacks for that particular scenario.

What should I do to tackle this kind of scenario?

like image 498
Aritra Roy Avatar asked Jun 15 '16 08:06

Aritra Roy


People also ask

Can I use Firebase without Internet?

By enabling persistence, any data that the Firebase Realtime Database client would sync while online persists to disk and is available offline, even when the user or operating system restarts the app. This means your app works as it would online by using the local data stored in the cache.

What does getKey () do Firebase?

Calling the getKey() method on this reference will return the auto-generated key which may then be used to store a corresponding value. Using Firebase to generate unique keys in this way is also of particular use when an app has multiple users creating child nodes at the same path in the tree.

How do I manually add data to Firebase realtime database?

If you open your app from Firebase dashboard, you can add data manually by clicking on the + sign.


1 Answers

Let's see how those methods are triggered when there is no connectivity:

onCancelled - when there is a server-side error. For example, when the user doesn't have access to the specified node. (Or when you reach the connection limit on the Sparkle plan).

onDataChange - if there is data persisted, it will read this data. If not, this method won't be triggered (your case).

According to the Firebase Documentation, If you want to check if the device is connected to the Firebase Server, you can add a listener to .info/connected. Like this:

DatabaseReference connectedRef = FirebaseDatabase.getInstance().getReference(".info/connected");
connectedRef.addValueEventListener(new ValueEventListener() {
  @Override
  public void onDataChange(DataSnapshot snapshot) {
    boolean connected = snapshot.getValue(Boolean.class);
    if (connected) {
      Toast.makeText(this, "Connected", Toask.LENGTH_SHORT);
    } else {
      Toast.makeText(this, "Not connected", Toask.LENGTH_SHORT).show();
    }
  }

  @Override
  public void onCancelled(DatabaseError error) {
  }
});
like image 144
Rosário Pereira Fernandes Avatar answered Sep 28 '22 05:09

Rosário Pereira Fernandes