0

I'm implementing Firebase java sdk for a backend. I thought by listening to firebase change event that the program should stay open but it doesn't

 final FirebaseDatabase database = FirebaseDatabase.getInstance();
 DatabaseReference ref = database.getReference(config.getFirebase().getIncomingPath()); 
 ref.limitToFirst(1).addChildEventListener( new ChildEventListener() {

            @Override
            public void onChildRemoved(DataSnapshot snapshot) {

            }

            @Override
            public void onChildMoved(DataSnapshot snapshot, String arg1) {
            }

            @Override
            public void onChildChanged(DataSnapshot snapshot, String arg1) {

            }

            @Override
            public void onChildAdded(DataSnapshot snapshot, String arg1) {                  
                System.out.println(snapshot);

            }

            @Override
            public void onCancelled(DatabaseError arg0) {
                System.err.println("done");

            }
 });

When doing this the program terminates even if there are data in the paths.

I have to do something like this to keep the program alive.

Thread someThread = new Thread(new Runnable() {     
    @Override
    public void run() {
      while (true) {
        try {
            Thread.sleep(10000);
        } catch (InterruptedException e) {
        }
    }           
  }
});
someThread.start();

Is there something in the SDK that I have missed?

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
Edgesoft
  • 282
  • 2
  • 16
  • 1
    A `Thread.sleep()` is indeed the simple way to test this, but the proper solution is to use semaphores to signal when you're done. See the answer I linked. – Frank van Puffelen Jan 11 '17 at 14:36
  • @FrankvanPuffelen Thank you! What should I do with this topic? Should it still be here when it is a duplicate or should I remove it? – Edgesoft Jan 11 '17 at 16:44
  • It's fine to keep it around. Someone might stumble upon this, instead of the dupe and still be helped by having this question. – Frank van Puffelen Jan 11 '17 at 18:55

0 Answers0