Android – how to separate firestore listeners

I'm new to firestore. I created a listener registration to update my recycler view. I know my implementation may not be perfect, but every time my activity is destroyed, my application will cause an error on the line in this listener. I don't know why, but MT registration. Remove() is before destroy or finish() There was no work after the event. Can anyone help?

private ListenerRegistration registration;
private com.google.firebase.firestore.Query query;
private void requestPacienteList(){
    FirebaseFirestore db = FirebaseFirestore.getInstance();
    progress.setVisibility(View.VISIBLE);
    query = db.collection("Hospital");
    registration = query.addSnapshotListener(new EventListener<QuerySnapshot>() {
        @Override
        public void onEvent(QuerySnapshot documentSnapshots, FirebaseFirestoreException e) {
            for(DocumentSnapshot documentSnapshot : documentSnapshots){
                if(documentSnapshot.get("nome").equals("Santa Clara")){
                    hospital = documentSnapshot.toObject(Hospital.class);
                    hospital.setHospitalDocumentKey(documentSnapshot.getId());
                    FirebaseFirestore db = FirebaseFirestore.getInstance();
                    db.collection("Hospital").document(hospital.getHospitalDocumentKey()).collection("Pacientes").addSnapshotListener(new EventListener<QuerySnapshot>() {
                        @Override
                        public void onEvent(QuerySnapshot documentSnapshots, FirebaseFirestoreException e) {
                            homeModelList.clear();
                            for(DocumentSnapshot documentSnapshot : documentSnapshots){
                                final Paciente paciente = documentSnapshot.toObject(Paciente.class);
                                paciente.setPacienteKey(documentSnapshot.getId());
                                FirebaseFirestore db = FirebaseFirestore.getInstance();
                                db.collection("Pessoa").document(paciente.getProfissionalResponsavel()).addSnapshotListener(new EventListener<DocumentSnapshot>() {
                                    @Override
                                    public void onEvent(DocumentSnapshot documentSnapshot, FirebaseFirestoreException e) {
                                        Profissional profissional = documentSnapshot.toObject(Profissional.class);
                                        int[] covers = new int[]{
                                                R.drawable.ic_person_black};
                                        HomeModel p = new HomeModel(paciente.getNome()+" "+paciente.getSobrenome(),paciente.get@R_512_2419@(),paciente.getLeito(),
                                                covers[0],profissional.getNome()+ " "+profissional.getSobrenome(),paciente.getPacienteKey());
                                        homeModelList.add(p);
                                        homeAdapter.notifyDataSetChanged();
                                        prepareListaPacientes();
                                    }
                                });
                            }
                        }
                    });
                }
            }
        }
    });
}

switch (id){
                case R.id.logout:
                    if(FirebaseAuth.getInstance().getCurrentUser()!=null)
                        FirebaseAuth.getInstance().signOut();
                    Intent it = new Intent(HomeActivity.this, MainActivity.class);
                    startActivity(it);
                    if(registration!=null)
                        registration.remove();
                    finish();
                    drawerLayout.closeDrawers();
                    break;
            }

My ondestroy method:

@Override
protected void onDestroy() {
    super.onDestroy();
    registration.remove();
}

When I delete this if:

if(FirebaseAuth.getInstance().getCurrentUser()!=null)
                        FirebaseAuth.getInstance().signOut();

My problem disappears. But if I don't, I will receive the following error:

java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.Object com.google.firebase.firestore.DocumentSnapshot.toObject(java.lang.Class)' on a null object reference
                                                            at santauti.app.Activities.Home.HomeActivity$4$1$1.onEvent(HomeActivity.java:200)
                                                            at santauti.app.Activities.Home.HomeActivity$4$1$1.onEvent(HomeActivity.java:197)
                                                            at com.google.firebase.firestore.DocumentReference.zza(UnkNown Source)
                                                            at com.google.firebase.firestore.zzd.onEvent(UnkNown Source)
                                                            at com.google.android.gms.internal.zzejz.zza(UnkNown Source)
                                                            at com.google.android.gms.internal.zzeka.run(UnkNown Source)
                                                            at android.os.Handler.handleCallback(Handler.java:751)
                                                            at android.os.Handler.dispatchMessage(Handler.java:95)
                                                            at android.os.Looper.loop(Looper.java:154)
                                                            at android.app.ActivityThread.main(ActivityThread.java:6119)
                                                            at java.lang.reflect.Method.invoke(Native Method)
                                                            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
                                                            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)

resolvent:

When you call use addsnapshotlistener you attach a listener to make any changes. Obviously, you must go to detach those listeners before the activity is destroyed. Another method is to add the activity to your call to addsnapshotlistener:

 db.collection("Pessoa").document(paciente.getProfissionalResponsavel())
   .addSnapshotListener(MainActivity.this, new EventListener<DocumentSnapshot>() {

You need to update mainactivity.this to match your code

By passing in an activity, firestore can automatically clean up listeners when the activity stops

Another alternative is to use get () to get those nested documents. It only reads the documents once. Because it is read only once, there is no audience cleaning

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>