0

I am trying to avoid the asynchronous layout pefromaed naturally with Firebase. so I am trying to complete my method in the one dataonchanged method.

I need to know is it possible and how i can reference 2 nodes as you can see here, using the one reference. iv tried something like

dataBaseLastIn = FirebaseDatabase.getInstance().getReference("clockInDate" + "clockOutDate");

//and

dataBaseLastIn = FirebaseDatabase.getInstance().getReference("clockInDate");
dataBaseLastIn = FirebaseDatabase.getInstance().getReference("clockOutDate");

//Method that works to set text to one reference but not both

protected void onStart() {
        super.onStart();
        final String uid = FirebaseAuth.getInstance().getCurrentUser().getUid();
        dataBaseLastIn.addValueEventListener(new ValueEventListener(){

            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {
                dateinList.clear();
                dateOutList.clear();
                for (DataSnapshot postSnapshot : dataSnapshot.getChildren()) {
                    Date date = postSnapshot.getValue(Date.class);
                    String datt = date.getDate().toString().trim();
                    String user = date.getCurrUser().toString().trim();
                    if (DateNew.equals(datt) && user.equals(uid)) {
                        dateinList.add(date);
                    } else {
                        dateinList.remove(date);
                    }

                    DateOut dateOut = postSnapshot.getValue(DateOut.class);
                    String dattOut = dateOut.getDate().toString().trim();
                    String userOut = dateOut.getCurrUser().toString().trim();
                    if (DateNew.equals(dattOut) && userOut.equals(uid)) {
                        dateOutList.add(dateOut);
                    } else {
                        dateOutList.remove(date);
                    }
                }

                newList = dateinList;
                outList = dateOutList;
                try {
                    totalINList();
                } catch (ParseException e) {
                    e.printStackTrace();
                }
            }
            @Override
            public void onCancelled(DatabaseError databaseError) {
            }
        });

    }

//setting the text

 public void totalINList() throws ParseException {
        for(int i = 0 ; i < newList.size(); i++) {
            timeDuration.add(newList.get(i).getClockTime());
        }
        Date e = newList.get(newList.size() - 1);
        String timeIn = e.getClockTime();
        txtCurrentTime.setText(timeIn);

        for(int i = 0 ; i < outList.size(); i++) {
            timeDurationOut.add(outList.get(i).getTime());
        }
        DateOut f = outList.get(outList.size() - 1);
        String timeOut = f.getTime();
        txtCurrentTimeOut.setText(timeOut);
    }

May be something simple but i only get the value of the last reference i declare which obviously makes sense but was wondering is ther a way i can reference both nodes in the one

Alex Mamo
  • 130,605
  • 17
  • 163
  • 193
James Ferry
  • 127
  • 1
  • 3
  • 12
  • Possible duplicate of [How to handle asynchronous Database with Firebase?](https://stackoverflow.com/questions/48720701/how-to-handle-asynchronous-database-with-firebase) – Peter Haddad Feb 21 '18 at 17:44
  • https://medium.com/google-developers/why-are-the-firebase-apis-asynchronous-e037a6654a93 – Doug Stevenson Feb 21 '18 at 18:00

1 Answers1

0

No, you cannot achieve something like with Firebase.

The initial response for most developers is to try and "fix" this asynchronous behavior, which I personally recommend against this. The web is asynchronous, and the sooner you accept that, the sooner you can learn how to become productive with modern web APIs.

Alex Mamo
  • 130,605
  • 17
  • 163
  • 193
  • I defiantly agree, I was just short on time so hoped there may have been a quick fix/(work around). I can resolve it by putting all the data under the one node in Fire-base but again its not good practice. I will read up a bit more on asynchronous, I know it will benefit me more in the long run – James Ferry Feb 21 '18 at 17:58
  • For a better understanding, take a look at my answer from this [post](https://stackoverflow.com/questions/47847694/how-to-return-datasnapshot-value-as-a-result-of-a-method/47853774). – Alex Mamo Feb 21 '18 at 18:30
  • Thanks for the link, I re-framed the code in the ondata changed and linked 2 entities together in another table in the database so was able to get it that way. Thanks for your help, much appreciated :-) – James Ferry Feb 26 '18 at 17:37