找出查询何时完成加载最新添加的项目

时间:2017-03-21 20:39:28

标签: android firebase firebase-realtime-database

我有一个将数据写入数据库的功能

private void startCommenting() {

    final String comment_val = meditComment.getText().toString().trim();

    meditComment.setText("");

    if (!TextUtils.isEmpty(comment_val)) {

        mProgress.show();

        final DatabaseReference newPost = mComment.child(post_key).push();
        final String commentkey = newPost.getKey();

        mUser.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {

                Map<String,Object> checkoutData=new HashMap<>();
                checkoutData.put("time",ServerValue.TIMESTAMP);
                newPost.setValue(checkoutData);
                newPost.child("comment").setValue(comment_val);
                newPost.child("uid").setValue(dataSnapshot.child("id").getValue());
                newPost.child("blogpost").setValue(dataSnapshot.child("blogkey").getValue());
                newPost.child("userimage").setValue(dataSnapshot.child("image").getValue());
                newPost.child("username").setValue(dataSnapshot.child("name").getValue());

            }
            @Override
            public void onCancelled(DatabaseError databaseError) {
            }
        });

    }
}

调用此函数后,会进行查询以获取包含子项中正确post_key的数据(&#34; blogpost&#34;)。

mpostComment.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

            startCommenting();

                mQueryCurrentComment = mComment.child(post_key).orderByChild("blogpost").equalTo(post_key);

                mQueryCurrentComment.addValueEventListener(new ValueEventListener() {
                    @Override
                    public void onDataChange(DataSnapshot dataSnapshot) {
                        String currentuserid;
                        String lastuserid = "";
                        String currentcommentuid;
                        for (DataSnapshot dsp : dataSnapshot.getChildren()) {
                            currentuserid = dsp.child("uid").getValue().toString();

                            Log.d(TAG, "user newid: " + currentuserid);
                            Log.d(TAG, "user oldid: " + lastuserid);


                            if (currentuserid.equals(lastuserid)) {

                            } else {
                                final DatabaseReference newCommentLike = mComment.child(currentuserid).push();
                                Map<String, Object> checkTime = new HashMap<>();
                                checkTime.put("time", ServerValue.TIMESTAMP);
                                newCommentLike.setValue(checkTime);
                                newCommentLike.child("location").setValue(location_key);
                                newCommentLike.child("category").setValue(category_key);
                                newCommentLike.child("pressed").setValue("false");
                                newCommentLike.child("message").setValue(" has also commented your post. ");
                                newCommentLike.child("blogpost").setValue(post_key);
                                newCommentLike.child(post_key).setValue(true);
                            }
                            lastuserid = currentuserid;
                        }
                    }

                    @Override
                    public void onCancelled(DatabaseError databaseError) {

                    }
                });  
        }
    });

但是,查询被触发两次,一次是在添加新项目之前,另一次是在添加新项目之后,如下所示:

Query Log

如何在添加最新项目后执行查询内部的操作,而不是两次?任何帮助表示赞赏!

0 个答案:

没有答案