gpt4 book ai didi

java - 如何停止 RecyclerView 适配器内的快照监听器?

转载 作者:行者123 更新时间:2023-12-02 09:27:20 26 4
gpt4 key购买 nike

我只是遇到了有关 Firestore 快照监听器的某个问题。我是使用它的新手,我读到每次移动到另一个 Activity 时都必须停止快照监听器。我尝试过:

db.collection("Mydata").document(userID).addSnapshotListener(itemView.getContext(), new EventListener<DocumentSnapshot>() {
@Override
public void onEvent(@Nullable DocumentSnapshot documentSnapshot, @Nullable FirebaseFirestoreException e) {

}
})

但是由于 itemView.getContext() 它有一个错误。

我的问题是:

  • 如果我不包含 Activity 并保留 snapshotlistener 这样可以吗?
  • 如果不行,有什么办法可以解决这个问题吗?

最佳答案

Is it okay if I don't include the Activity in and leave the snapshotlistener like that?

不,不是!当您为 listening to realtime updates 调用 addSnapshotListener 时,这意味着您附加一个监听器,该监听器会在数据库中发生的每个更改时被调用。因此,当您的应用程序关闭时也会发生这种情况,这就是为什么必须 detach the listeners在 Activity 被销毁之前。

If not, Is there any way to solve this?

是的,有。请参阅我在以下帖子中的回答:

If in your app you don't need to get the data in realtime, then you can simply use a get() call directly on the reference, which just reads the document only once. Since it only reads once, there is no listener to be removed.

关于java - 如何停止 RecyclerView 适配器内的快照监听器?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58254228/

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