gpt4 book ai didi

android - 如何分离 firestore 监听器

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:49:41 28 4
gpt4 key购买 nike

我是 FireStore 的新手。我创建了一个 ListenerRegistration 来更新我的 Recycler View。我知道我的实现可能并不完美,但每次我的 Activity 被销毁时,我的应用程序都会在此 Listener 内的行上抛出错误。我不知道为什么,但是 mt registration.remove() 在销毁之前或 finish() Activity 之后不起作用。有人可以帮忙吗?

public class MainActivity extends AppCompatActivity {

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.getBox(),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 remove this if:

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

我的问题解决了。但如果我不这样做,我会收到以下错误:

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)

最佳答案

当你use addSnapshotListener您附加一个监听器,该监听器会被要求进行任何更改。显然你必须 detach those listeners在 Activity 被销毁之前。另一种选择是 add the activity to your call to addSnapshotListener :

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

您需要更新 MainActivity.this 以匹配您的代码。

通过传入activity,Firestore可以在activity停止时自动清理监听器。

另一种选择是使用 get()获取那些嵌套的文档,它只读取文档一次。由于它只读取一次,因此没有需要清理的监听器。

关于android - 如何分离 firestore 监听器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46756915/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com