Firebase.database.getReference(".info/connected") not working

Aperta
#1,499 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
4/5
Tempo stimato
3-5 giorni
Idoneità per principianti
25/100
Tipo di issue
Bug
Chiarezza
Da chiarire
Stato di attività
Ferma
Stack tecnologico
android, firebase, kotlin
Ambito
databases, mobile

Direzione di ricerca

Inizia con FirebaseConnection.checkConnection e il ValueEventListener di .info/connected nello snippet Kotlin dell’issue. Riproduci la disconnessione nell’ambiente indicato con Xiaomi Redmi Note 10s e Android 12 utilizzando le versioni SDK specificate, quindi analizza il callback di connessione insieme alla lettura del database. Il lavoro è completato quando la causa è riproducibile e la gestione della connessione non impedisce più le letture e i caricamenti segnalati.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

Step 1: Describe your environment
  • Android device: Xiaomi Redmi Note 10s
  • Android OS version: 12
  • Google Play Services version: 1.37 (my cellphone)
  • Firebase/Play Services SDK version: 31.1.1 && 4.3.14
Step 2: Describe the problem:

I have an app that uses Realtime Database, and I need to handle connection errors so I use Firebase.database.getReference(".info/connected") to know when I'm connected and when I'm not. everything works fine when i start or restart the app, but about a minute (sometimes more, never less) it disconnects from firebase, and the connection is always negative, i can't upload scores (i have a quiz) or make queries, it just disconnects without apparent reason.

Steps to reproduce:



Observed Results:
  • What happened? This could be a description, logcat output, etc.

At some point in the life cycle of the application in the logcate it appears to me that it has been disconnected for no reason, even without doing anything in the application, this logcat simply appears:

023-05-24 14:08:45.348 18112-18112 ContentValues com.example.geoquizmx W my Class: not connected
2023-05-24 14:08:45.348 18112-18112 ContentValues com.example.geoquizmx W callbackgetUserData byUid: false
2023-05-24 14:08:45.348 18112-18112 ContentValues com.example.geoquizmx W my Class: not connected
2023-05-24 14:08:45.348 18112-18112 ContentValues com.example.geoquizmx W callback getSinglerecordByUid: false
2023-05-24 14:08:45.348 18112-18112 ContentValues com.example.geoquizmx W my Class: not connected
2023-05-24 14:08:45.348 18112-18112 ContentValues com.example.geoquizmx W callback getSinglerecordByUid: false
2023-05-24 14:08:45.348 18112-18112 ContentValues com.example.geoquizmx W my Class: not connected
2023-05-24 14:08:45.348 18112-18112 ContentValues com.example.geoquizmx W callbackgetUserData byUid: false
2023-05-24 14:08:45.348 18112-18112 ContentValues com.example.geoquizmx W my Class: not connected
2023-05-24 14:08:45.348 18112-18112 ContentValues com.example.geoquizmx W callbackgetUserData byUid: false
2023-05-24 14:08:45.348 18112-18112 ContentValues com.example.geoquizmx W my Class: not connected
2023-05-24 14:08:45.348 18112-18112 ContentValues com.example.geoquizmx W callbackgetUserData byUid: false
2023-05-24 14:08:45.349 18112-18112 ContentValues com.example.geoquizmx W my Class: not connected
2023-05-24 14:08:45.349 18112-18112 ContentValues com.example.geoquizmx W callbackgetUserData byUid: false
2023-05-24 14:08:45.349 18112-18112 ContentValues com.example.geoquizmx W my Class: not connected
2023-05-24 14:08:45.349 18112-18112 ContentValues com.example.geoquizmx W callbackgetUserData byUid: false
2023-05-24 14:08:45.349 18112-18112 ContentValues com.example.geoquizmx W my Class: not connected
2023-05-24 14:08:45.349 18112-18112 ContentValues com.example.geoquizmx W callback getSinglerecordByUid: false

Expected Results:
  • What did you expect to happen?
Relevant Code:
// TODO(you): code here to reproduce the problem

class FirebaseConnection {
      private var connected: Boolean = false

      fun checkConnection(callback: (Boolean) -> Unit) {
          val connectedRef = Firebase.database.getReference(".info/connected")

          connectedRef.addValueEventListener(object : ValueEventListener {
              override fun onDataChange(snapshot: DataSnapshot) {
                  val isConnected:Boolean? = snapshot.getValue(Boolean::class.java)
                  if (isConnected == true) {
                      connected = true
                      Log.w(TAG, "my Class: connected")
                  } else if(isConnected == false){
                      Log.w(TAG, "my Class: not connected")
                      connected = false
                  }

                  callback(connected)
              }

              override fun onCancelled(error: DatabaseError) {
                  Log.w(TAG, "Listener was cancelled")
                  callback(false)
              }
          })
      }

}

override suspend fun getSingleRecordByUid(uid: String, typeRec: String): DataSnapshot? {
      return withContext(Dispatchers.IO){
          val myClass = FirebaseConnection()

          var connection: Boolean? = null
          myClass.checkConnection { isConnected ->
              Log.w(TAG, "callback getSinglerecordByUid: $isConnected")
              connection = isConnected
          }

          while (connection == null) {
              // Esperar hasta que se complete la llamada de retorno
              delay(10)
          }

          if(connection as Boolean) userInstance.child(uid).child(typeRec).get().await() else null
      }
  }
Lingua principale
Kotlin
Stelle
9.3k
Fork
7.4k
Merge medio
7g 9h
PR unite (30g)
6

Preparare l'ambiente

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di firebase/quickstart-android

Tutte le issue di firebase/quickstart-android

Issue simili

Altre issue su Kotlin

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.