使用Firebase ValueEventListener检测值更改 [英] Detect value change with firebase ValueEventListener

查看:124
本文介绍了使用Firebase ValueEventListener检测值更改的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从Firebase数据库检测值更改.这是我用于初始化ValueEventListener的代码:

I am trying to detect value change from my Firebase Database. Here is my code for initializing the ValueEventListener:

valueEventListener = new ValueEventListener() {
        @Override
        public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
            try {
                String customerLocation = String.valueOf(dataSnapshot.getValue());
                Point customerPoint = locationFounder.getPoint(customerLocation);

                if (customerPoint != null) {
                    databaseReference.child("isActive").addValueEventListener(new ValueEventListener() {
                        @Override
                        public void onDataChange(@NonNull DataSnapshot dataSnapshot) {

                            boolean isActive = Boolean.valueOf(String.valueOf(dataSnapshot.getValue()));

                            displayPopUp(isActive, customerPoint, customerLocation);
                        }

                        @Override
                        public void onCancelled(@NonNull DatabaseError databaseError) {

                        }
                    });
                }
            } catch (Exception e) {
                Log.d("Listener",e.toString());
            }
        }

        @Override
        public void onCancelled(@NonNull DatabaseError databaseError) {

        }
    };
    destinationReference.addValueEventListener(valueEventListener);

我想在活动中调用此侦听器时出现问题.我一直在尝试:

Problem occurs when I want to call this listener in my activity. I've been trying with this:

destinationListener.getValueEventListener().onDataChange(REQUIRED_SNAPSHOT);

我不知道如何获取onDataChange所需的datasnapshot.如果可能的话,我想使用ValueEventListener而不是ChildEventListener进行这项工作.但是,我不太确定这是否是检测价值变化的正确方法.如果还有其他方法可以正常工作,我想了解一下.

I do not know how can I get datasnapshot which is required for onDataChange. I would like to get this work with ValueEventListener, not ChildEventListener, if possible. However, I am not pretty sure that this is the right way of trying to detect value change. If there is any other way that will work properly, I'd like to know about it.

推荐答案

Firebase实时数据库没有内置内容可以告诉您快照中的哪些特定数据在 onDataChange 方法中已更改.

There is nothing built to the Firebase Realtime Database to tell you what specific data under the snapshot has changed in the onDataChange method.

如果您想知道哪些特定属性已更改,则需要:

If you want to know what specific property has change, you'll need to:

  1. 保留在 onDataChange 字段中获得的快照.
  2. 调用 onDataChange 时,将新快照中的数据与字段中的数据进行比较.
  1. Keep the snapshot that you get in onDataChange in a field.
  2. When onDataChange gets called, compare the data in the new snapshot with the data in the field.

假设您在JSON中的一个节点上有一个引用,并且在该节点下是一个 status 属性,并且您想同时侦听整个节点,并检测是否状态已更改.

Say that you have a reference on a node in your JSON, and under that node is a status property, and you want to both listen to the entire node, and detect if the status has changed.

您可以使用以下方法做到这一点:

You'd do that with something like:

// Add a field to your class to keep the latest snapshot
DataSnapshot previousSnapshot;

// Then add your listener
databaseReference.addValueEventListener(new ValueEventListener() {
    @Override
    public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
        bool wasActive = false;
        if (previousSnapshot != null && previousSnapshot.child("status").exists()) {
            wasActive = dataSnapshot.child("status").getValue(Boolean.class);
        }
        boolean isActive = dataSnapshot.child("status").getValue(Boolean.class);

        if (isActive <> wasActive) {
            ... the user's status changed
        }

        previousSnapshot = dataSnapshot;
    }

    @Override
    public void onCancelled(@NonNull DatabaseError databaseError) {
        throw databaseError.toException(); // never ignore errors
    }
});

这篇关于使用Firebase ValueEventListener检测值更改的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆