0

I'm using Firebase for my android project. There is something that I don't understand. I'm using a method addChildEventListener and for what i know this method should be triggered when a new child is added/deleted/changed and go on... For some reason when my Activity is load up, this method triggers. Isn't need to be triggered when a new child is added...?

Code

    refToVideos.getReference(Params.VIDEOS).child(currentUser.getUid()).addChildEventListener(new ChildEventListener() {
        @Override
        public void onChildAdded(DataSnapshot snapshot, String s) {
            long countComments  = (long)snapshot.child(Params.COUNTCOMMENTS).getValue();
            int countComment = ((int) countComments);
            String frameURL  = (String)snapshot.child(Params.FRAMEURL).getValue();
            String genre  = (String)snapshot.child(Params.GENRE).getValue();
            long like  = (long)snapshot.child(Params.LIKES).getValue();
            int likes = ((int)like);
            String uploadDate  = (String)snapshot.child(Params.UPLOADDATE).getValue();
            String userProfile  = (String)snapshot.child(Params.USERPROFILE).getValue();
            String userUID  = (String)snapshot.child(Params.USERUID).getValue();
            String userName  = (String)snapshot.child(Params.USERNAME).getValue();
            String videoID  = (String)snapshot.child(Params.VIDEOID).getValue();
            String videoName  = (String)snapshot.child(Params.VIDEONAME).getValue();
            String videoURL  = (String)snapshot.child(Params.VIDEOURL).getValue();
            long view  = (long)snapshot.child(Params.VIEWS).getValue();
            int views = ((int)view);

            Video video = new Video(videoID,userUID,genre,videoName,videoURL,frameURL,userName,userProfile,likes,
                    views,countComment,uploadDate);
            videosList.add(video);
            if(adapter != null)
            adapter.notifyDataSetChanged();
        }

        @Override
        public void onChildChanged(DataSnapshot dataSnapshot, String s) {

        }

        @Override
        public void onChildRemoved(DataSnapshot dataSnapshot) {

        }

        @Override
        public void onChildMoved(DataSnapshot dataSnapshot, String s) {

        }

        @Override
        public void onCancelled(DatabaseError databaseError) {

        }
    });
Anton Kazakov
  • 2,740
  • 3
  • 23
  • 34
E.Bolandian
  • 553
  • 10
  • 35

1 Answers1

1

From the documentation for onChildAdded:

This callback is triggered once for each existing child and then again every time a new child is added to the specified path.

So when you attach a listener, its onChildAdded method is 'immediately' called for any existing children in the location it listens to. This is how the API is defined, and you can't change it.

If you only want to hear about new children that were added after the moment you attach the listener, you'll have to have something like a timestamp in your child nodes that identifies whether they're 'new'.

Also see these previous questions about the topic:

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807