Hein Htet Aung
Hein Htet Aung

Reputation: 854

ValueEventListener triggered onDataChange method just once

I am facing a problem with Firebase Realtime Database in my Android app.

What I am trying is to update the list in Realtime. But the update only works on app start. It doesn't update automatically. So, I need to restart the app whenever want update.

Is there any missing part? I appreciate for any help. Thanks

Problem: Listener triggered onDataChange method only in app restart. Data are correct. But listener doesn't work after that.

Code:

mRef = FirebaseDatabase.getInstance().getReference();
mPostRef = mRef.child(DbUtils.POST_CHILD);
mPostRef.keepSynced(true);

mPostRef.addValueEventListener(new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
        List<Post> objList = new ArrayList<>();
        for(DataSnapshot objSnapShot: dataSnapshot.getChildren()){
            Post obj = objSnapShot.getValue(Post.class);
            obj.setKey(objSnapShot.getKey());
            objList.add(obj);
        }
        mAdapter.addAllAndNotify(objList);

    }

    @Override
    public void onCancelled(DatabaseError databaseError) {
        Log.i("Posts", "Listener was canceled");
    }
});

Upvotes: 1

Views: 1034

Answers (1)

Hein Htet Aung
Hein Htet Aung

Reputation: 854

Finally, I found the bug.

The fact is Firebase doesn't support multiple connection. So, I removed all other DatabaseReference from my activity. And it works now.

Thanks people who tried to help me.

Upvotes: 1

Related Questions