Correct use of the onChildRemoved method in Childeventlistener

Asked

Viewed 35 times

1

Good evening! I’m making a chat app and I list the conversations in a recyclerview. I chose to use the addChildEventListener method, because (I think) it will consume less resources since the data will only be updated if there is a change in firebase. I can add conversations normally but found a problem when deleting. When I delete one of the conversations my recyclerview with the chat list does not update and it keeps appearing in the list even after it is deleted from firebase. The list is only updated if I close and open my app again. I know I should do something within the onChildRemoved method, but I don’t know how to make the list update. This is my method.

public void recuperarConversasPareamento(){

    try {

        listaConversas.clear();
        childEventListenerChat = conversasParearExibicao.addChildEventListener(new ChildEventListener() {
            @Override
            public void onChildAdded(@NonNull DataSnapshot dataSnapshot, @Nullable String s) {
                Log.i("retornoChat", dataSnapshot.getValue().toString());

                for (DataSnapshot ds : dataSnapshot.getChildren()) {

                    Conversa conversa = ds.getValue(Conversa.class);
                    Log.i("retornoChat2", conversa.getUsuarioExibicao().getApelido());
                    Log.i("retornoChat2", conversa.getUsuarioExibicao().getEmail());
                    listaConversas.add(conversa);                    }

                adapter.notifyItemInserted(listaConversas.size());
                adapter.notifyDataSetChanged();
            }

            @Override
            public void onChildRemoved(@NonNull DataSnapshot dataSnapshot) {
                Conversa conversa = dataSnapshot.getValue(Conversa.class);
                listaConversas.remove(conversa);
                Log.i("log", "O dado foi removido");
                Log.i("retornoChatRemovido", dataSnapshot.getValue().toString());
                adapter.notifyDataSetChanged();
            } 
        });

    } catch (Exception e){
        Log.i("erro-recuperarListaChat", e.getMessage());
    }

And that’s the message structure I’m retrieving from firebase. inserir a descrição da imagem aqui

1 answer

0

I haven’t figured out the best way to use onChildRemoved yet, but as a stopgap measure I am updating Fragment manually whenever there is a removal. For this I added the lines of code within the onChildRemoved method because it is only triggered when a child is removed. Lines to update Fragment(or your Activity) can also be placed after your method of removing the item because it ends up in it.

@Override
            public void onChildRemoved(@NonNull DataSnapshot dataSnapshot) {
                Log.i("log", "REMOVIDO COM SUCESSO");
                Log.i("dadoRemovido", dataSnapshot.getValue().toString());

                /*Atualiza a tela do fragment após remover*/
                FragmentTransaction ft = getFragmentManager().beginTransaction();
                ft.detach(ChatFragment.this).attach(ChatFragment.this).commit();

            }

Browser other questions tagged

You are not signed in. Login or sign up in order to post.