Firebase数据库不停止更新数值。

huangapple go评论92阅读模式
英文:

Firebase database Doesn't stop updating value

问题

我正在尝试创建一个聊天应用程序,并尝试检查用户是否已经看过一条消息。对于首先发送消息的用户,该应用程序运行正常,但对于在第一个用户之后发送消息的用户,它不断更新。

这是数据库结构:

Firebase数据库不停止更新数值。

因此,值seen在第二个用户发送的MFRVfDa3JrFi8Gg9aL2消息中不断更新。

这是代码:

ValueEventListener seenListener;
private void seenMessage(String qid) {
    db = FirebaseDatabase.getInstance().getReference().child("Message");

    seenListener = db.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(@NonNull DataSnapshot snapshot) {
            if (snapshot.exists()) {
                for (DataSnapshot dataSnapshot : snapshot.getChildren()) {
                    for (DataSnapshot childdshot : dataSnapshot.getChildren()) {
                        Chats chat = childdshot.getValue(Chats.class);
                        if (chat.getReceiverID().equals(userId) && chat.getSenderId().equals(qid)) {
                            HashMap<String, Object> hashMap = new HashMap<>();
                            hashMap.put("seen", true);
                            childdshot.getRef().updateChildren(hashMap);
                        }
                    }
                }
            }

        }
        @Override
        public void onCancelled(@NonNull DatabaseError error) {

        }
    });

}

那么,我应该怎么做才能阻止它不断自我更新呢?

英文:

I am trying to create a chat app and I am trying to check if the user has seen a message or not. The app works fine for the user who has sent the message first but for the user who has sent the message after the first user, it keeps updating.

Here is the database structure

Firebase数据库不停止更新数值。

So, The value seen keeps updating between true and false for MFRVfDa3JrFi8Gg9aL2 which was sent by the second user.

Here is the Code

ValueEventListener seenListener;
private void seenMessage(String qid) {
    db = FirebaseDatabase.getInstance().getReference().child(&quot;Message&quot;);

    seenListener = db.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(@NonNull DataSnapshot snapshot) {
            if (snapshot.exists()) {
                for (DataSnapshot dataSnapshot : snapshot.getChildren()) {
                    for (DataSnapshot childdshot : dataSnapshot.getChildren()) {
                        Chats chat = childdshot.getValue(Chats.class);
                        if (chat.getReceiverID().equals(userId) &amp;&amp; chat.getSenderId().equals(qid)) {
                            HashMap&lt;String, Object&gt; hashMap = new HashMap&lt;&gt;();
                            hashMap.put(&quot;seen&quot;, true);
                            childdshot.getRef().updateChildren(hashMap);
                        }
                    }
                }
            }

        }
        @Override
        public void onCancelled(@NonNull DatabaseError error) {

        }
    });

}

So, What should I do to stop it from updating itself again and again?

答案1

得分: 3

你的 addValueEventListener 会立即触发当前值,并在每次发生更改时再次触发。因为你在 onDataChange 中更改了值,这会再次触发监听器,一次又一次地触发……

如果只想读取值一次并进行更新,你应该使用 addListenerForSingleValueEvent,而不是 addValueEventListener

英文:

Your addValueEventListener gets triggered immediately with the current value, and then every time something changes. Since you change the value in onDataChange, that triggers the listener again, and again, and...

To only read the value once and update it you'll want to use addListenerForSingleValueEvent instead addValueEventListener.

huangapple
  • 本文由 发表于 2020年8月24日 02:25:04
  • 转载请务必保留本文链接:https://go.coder-hub.com/63550590.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定