Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Accessing parent object upon detecting a change in just a sub node value

I have an Order node and a Status key under that. I am looking for changes in the Status’s value and if there’s any change detected, I have to read the parent Order Node.

The key point is I do not want to listen to changes in the Order node. But just the Status under the Order node. What is the most optimum way to do this? Is it good to have a listener inside another listener? Here's my code:

 final Query qRef = FirebaseDatabase.getInstance().getReference().child("orders").orderByChild("customerId").equalTo(FirebaseAuth.getInstance().getCurrentUser().getUid())
            .limitToFirst(1);
    qRef.keepSynced(true);

    final DatabaseReference reference = qRef.getRef().child("status");


    reference.addValueEventListener(new ValueEventListener() {

        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {


            DatabaseReference inner = reference.getParent();

            inner.addValueEventListener(new ValueEventListener() {
                @Override
                public void onDataChange(DataSnapshot parent) {
                    if (parent != null) {
                        Order newOrder = parent.getValue(Order.class);
                        if (newOrder != null) {
                            if (newOrder.getStatus() != null && !newOrder.getStatus().equals("FX_OVER"))
                                processOrder(newOrder);
                        }
                    }
                }

                @Override
                public void onCancelled(DatabaseError databaseError) {

                }
            });
        }

        @Override
        public void onCancelled(DatabaseError databaseError) {

        }

    });

And the JSON:

{   "20161010210955" : {
    "createdAt" : {
      "date" : 10,
      "day" : 1,
      "hours" : 21,
      "minutes" : 9,
      "month" : 9,
      "seconds" : 55,
      "time" : 1476113995244,
      "timezoneOffset" : -330,
      "year" : 116
    },
    "customerId" : "npsd3Ldfsdf1x0hsdfsdf2mj14VUA876f2",
    "id" : "20161010210955",
    "status" : "FX_FULL"

  } }
like image 919
Vinod Avatar asked Oct 10 '16 17:10

Vinod


1 Answers

Why don't you check status first? You know the value of status at beginning. I think this solution should be optimum way.

reference.addValueEventListener(new ValueEventListener() {

    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {

        if("FX_OVER".equals(dataSnapshot.getValue(String.class)) {

            reference.getParent().addListenerForSingleValueEvent(new ValueEventListener() {
                @Override
                public void onDataChange(DataSnapshot parent) {
                    // This is a defensive check. I dont know this is necessary.
                    // If status exist, parent must be exist.
                    if (parent.exists()) {
                        Order newOrder = parent.getValue(Order.class);
                        if (newOrder != null) {
                            processOrder(newOrder);
                        }
                    }
                }

                @Override
                public void onCancelled(DatabaseError databaseError) {}
            });
        }
    }

    @Override
    public void onCancelled(DatabaseError databaseError) {}
});
like image 174
Muzaffer Avatar answered Nov 15 '22 01:11

Muzaffer