Home > OS >  Firebase behaves strangely
Firebase behaves strangely

Time:01-23

This video explains what is happening with Firebase, after entering the following code. Until the second 5 of the video, everything is normal. When I click on a button from the 6th second, Firebase gets this video problem:

https://www.youtube.com/watch?v=rQlTZyCWcmM

Code:

        button = findViewById(R.id.button);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                final String raça = spinner_raca.getSelectedItem().toString();
                final String regiaoDoEstado = spinner_regiaoDoEstado.getSelectedItem().toString();
                final String estado = spinner_estado.getSelectedItem().toString();
                String identificador2 = identificadorCampo2.getText().toString();
                String identificadorEmHash = criptografarEmMD5(identificador2);

                Intent intentRecebedora = getIntent();
                Bundle bundleRecebedor = intentRecebedora.getExtras();
                String nomecompleto = bundleRecebedor.getString("nomecompleto");
                String email = bundleRecebedor.getString("email");
                String doencasPreExistentes = bundleRecebedor.getString("doencasPreExistentes");
                String dataDeNascimento = bundleRecebedor.getString("dataDeNascimento");
                String genero = bundleRecebedor.getString("genero");
                String identificadorEmHash = bundleRecebedor.getString("identificadorEmHash");
                String telefone = intentRecebedora.getStringExtra("telefone");

                referencia = FirebaseDatabase.getInstance().getReference().child("Usuarios").child(identificadorEmHash);
                Toast.makeText(getApplicationContext(), identificadorEmHash, Toast.LENGTH_LONG).show();
                if(identificadorEmHash.equals(null)) {
                    Toast.makeText(getApplicationContext(), "Identifier does not exist", Toast.LENGTH_LONG).show();
                } else {
                    referencia.addValueEventListener(new ValueEventListener() {
                        @Override
                        public void onCancelled(@NonNull DatabaseError error) {

                        }
                        @Override
                        public void onDataChange(@NonNull DataSnapshot dataSnapshot) {

                            referencia = FirebaseDatabase.getInstance().getReference().child("Usuarios").child(identificadorEmHash);
                            if (!dataSnapshot.exists()) {
                                Toast.makeText(getApplicationContext(), "O identificador digitado não existe.", Toast.LENGTH_LONG).show();
                            } else {
                                HashMap<String, String> sendToDataBase = new HashMap<>();
                                    sendToDataBase.put("Nome Completo", nomecompleto);
                                    sendToDataBase.put("E-mail", email);
                                    sendToDataBase.put("Identificador em Hash", identificadorEmHash);
                                    sendToDataBase.put("Telefone", telefone);
                                    sendToDataBase.put("Doenças Pré Existentes", doencasPreExistentes);
                                    sendToDataBase.put("Data de Nascimento", dataDeNascimento);
                                    sendToDataBase.put("Gênero", genero);
                                    sendToDataBase.put("Região do Estado em que mora", regiaoDoEstado);
                                    sendToDataBase.put("Raça", raça);
                                    sendToDataBase.put("Estado do Brasil em que mora", estado);

                                try {
                                    raiz.child(identificadorEmHash).setValue(sendToDataBase);

                                } catch (Exception e) {
                                    e.getMessage();
                                }

                            }
                           
                        }
                    });
                }
            }
        });

Please, how to fix this? Is there any location in the Firebase where we can see the errors? No errors are pointed out in Android Studio, and the phone system crashes, having to be restarted.

CodePudding user response:

When you are using the Query#addValueEventListener() method, it means that you are listening for real-time updates (changes). That being said, every change that takes place in the Realtime Database that corresponds to your referencia query, will always trigger the onDataChange() method. Since when you are adding data inside the callback to the query you are listening to, your sendToDataBase gets written again, hence that behavior. This is happening over and over again.

To solve this, simply change the above method call to Query#addListenerForSingleValueEvent(). This means that it will listen for changes only once.

  •  Tags:  
  • Related