Firebase gets stuck on no INTERNET connection

前端 未结 1 1700
鱼传尺愫
鱼传尺愫 2021-02-02 13:20

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

相关标签:
1条回答
  • 2021-02-02 13:29

    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) {
      }
    });
    
    0 讨论(0)
提交回复
热议问题