Firebase onChildChanged Listener被解雇了两次

时间:2017-11-08 07:00:59

标签: java android firebase firebase-realtime-database android-recyclerview

我面临着firebase非常奇怪的问题,我在firebase中有附加格式的数据,我试图获取数据重新创建整个recyclerview适配器列表,如果有任何子节点对话改变了。

如果我使用调试器运行它,它只会触发onChildChanged()一次,否则会触发两次,请帮忙。

private void getConversations(){

    final DatabaseReference conversationRef = databaseReference.child("conversation").child(currentUser);

    // Get event fired when new child added
    conversationRef.orderByChild("time").addChildEventListener(new ChildEventListener() {
        @Override
        public void onChildAdded(DataSnapshot dataSnapshot, String prevChildKey) {
            String ref= dataSnapshot.getKey();
            int position = userListRef.size();
            userListRef.add(position,ref);
            getDetailedConversations(position);
        }

        @Override
        public void onChildChanged(DataSnapshot dataSnapshot, String prevChildKey) {
                userListRef.clear();
                conversationListRef.clear();
                conversationAdapter.clear();
                //conversationAdapter.notifyDataSetChanged();
                Log.d(TAG, "new position: data changed+++++++++++++++++++++");
                conversationRef.removeEventListener(this);
                getConversations();

        }

        @Override
        public void onChildRemoved(DataSnapshot dataSnapshot) {}

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

        @Override
        public void onCancelled(DatabaseError databaseError) {}


    });
}

private void getDetailedConversations(final int position){

    DatabaseReference databaseReferenceAdapter = FirebaseDatabase.getInstance().getReference();
    final DatabaseReference conversationRef  = databaseReferenceAdapter.child("conversation").child(currentUser).child(userListRef.get(position));

    conversationRef.addListenerForSingleValueEvent(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            Log.d(TAG,"Single value event");
            final Conversation conversation = dataSnapshot.getValue(Conversation.class);
            conversation.setFirebaseToken(dataSnapshot.getKey());
            conversationListRef.add(conversation);
            conversationAdapter.notifyDataSetChanged();
        }

        @Override
        public void onCancelled(DatabaseError databaseError) {

        }
    });
}

LogCat:

11-07 22:56:01.127 15524-15577/io.ace.aceme E/Surface:
getSlotFromBufferLocked: unknown buffer: 0x9b9e2860
11-07 22:56:02.618 15524-15573/io.ace.aceme V/FA: Inactivity, 
disconnecting from AppMeasurementService
11-07 22:56:05.566 15524-15524/io.ace.aceme D/MessagesActivity: new 
position: data changed+++++++++++++++++++++
11-07 22:56:05.570 15524-15524/io.ace.aceme D/MessagesActivity: 
Single value event
11-07 22:56:05.785 15524-15524/io.ace.aceme D/MessagesActivity: new 
position: data changed+++++++++++++++++++++
11-07 22:56:05.785 15524-15524/io.ace.aceme D/MessagesActivity: 
Single value event
11-07 22:56:05.786 15524-15524/io.ace.aceme D/MessagesActivity: 
Single value event
11-07 22:56:06.134 15524-15524/io.ace.aceme D/MessagesActivity: 
Single value event
11-07 22:56:06.134 15524-15524/io.ace.aceme D/MessagesActivity: 
Single value event
11-07 22:56:06.137 15524-15524/io.ace.aceme D/MessagesActivity: 
Single value event
11-07 22:56:07.393 15524-15573/io.ace.aceme V/FA: Activity paused, 
time: 16426758
11-07 22:56:07.398 15524-15573/io.ace.aceme V/FA: Activity resumed, 
time: 16426764

Firebase数据库:

数据库图片

image1

1 个答案:

答案 0 :(得分:0)

这是更新的查询,无需使用orderBy

private void getConversations(){

final DatabaseReference conversationRef = databaseReference.child("conversation").child(currentUser);

// Get event fired when new child added
conversationRef.addChildEventListener(new ChildEventListener() {
    @Override
    public void onChildAdded(DataSnapshot dataSnapshot, String prevChildKey) {
        String ref= dataSnapshot.getKey();
        int position = userListRef.size();
        userListRef.add(position,ref);
        getDetailedConversations(position);
    }

    @Override
    public void onChildChanged(DataSnapshot dataSnapshot, String prevChildKey) {
            //Here no need to clear and reload
            //just find the user in list and update the corresponding object and call notifyDatasetChanged()

    }

    @Override
    public void onChildRemoved(DataSnapshot dataSnapshot) {}

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

    @Override
    public void onCancelled(DatabaseError databaseError) {}


});

}