更新 Firebase 时间戳数值

huangapple go评论75阅读模式
英文:

Update Firebase Timestamp Value

问题

mRootRef.child("Chat").child(mCurrentUserId).addValueEventListener(new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
        if (!dataSnapshot.hasChild(mChatUser)){
            Map chatAddMap = new HashMap();
            chatAddMap.put("seen", false);
            chatAddMap.put("uid", mChatUser);
            chatAddMap.put("timestamp", ServerValue.TIMESTAMP);

            Map chatUserMap = new HashMap();
            chatUserMap.put("Chat/" + mCurrentUserId + "/" + mChatUser, chatAddMap);
            chatUserMap.put("Chat/" + mChatUser + "/" + mCurrentUserId, chatAddMap);

            mRootRef.updateChildren(chatUserMap, new DatabaseReference.CompletionListener() {
                @Override
                public void onComplete(DatabaseError databaseError, DatabaseReference databaseReference) {
                    if (databaseError != null){
                        Log.d("CHAT LOG", databaseError.getMessage().toString());
                    }
                }
            });
        } else {
            mRootRef.child("Chat").child(mCurrentUserId).child(mChatUser).child("timestamp").setValue(ServerValue.TIMESTAMP);
        }
    }

    @Override
    public void onCancelled(DatabaseError databaseError) {

    }
});
英文:

I want to update the initial server timestamp value stored in my firebase database, so i did it in such a way that the timestamp value will be updated subsequently, but this seems to be causing major problem when i run the app, at first it works fine but after changing the value twice then it just freezes like the timestamp is constantly updated. I don.t know how i can make it perfect.

Below is the code were i add and want to change the timestamp value.

  mRootRef.child("Chat").child(mCurrentUserId).addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            if (!dataSnapshot.hasChild(mChatUser)){
                Map chatAddMap = new HashMap();
                chatAddMap.put("seen",false);
                chatAddMap.put("uid",mChatUser);
                chatAddMap.put("timestamp", ServerValue.TIMESTAMP);

                Map chatUserMap = new HashMap();
                chatUserMap.put("Chat/" + mCurrentUserId + "/" + mChatUser,chatAddMap);
                chatUserMap.put("Chat/" + mChatUser + "/" + mCurrentUserId,chatAddMap);

                mRootRef.updateChildren(chatUserMap, new DatabaseReference.CompletionListener() {
                    @Override
                    public void onComplete(DatabaseError databaseError, DatabaseReference databaseReference) {

                        if (databaseError != null){
                            Log.d("CHAT LOG", databaseError.getMessage().toString());
                        }

                    }
                });
            }else {

                mRootRef.child("Chat").child(mCurrentUserId).child(mChatUser).child("timestamp").setValue(ServerValue.TIMESTAMP);
            }
        }

        @Override
        public void onCancelled(DatabaseError databaseError) {


        }
    });

答案1

得分: 0

我终于找到了解决方案,我觉得我应该在这里发布一下,因为这个问题似乎在网上没有任何解决方案。

所以这里发生的情况是,我没有为需要在更新“时间戳”之前满足条件的情况添加检查,从而导致无休止的循环,因为它不知道何时停止。

所以我通过简单地将这段代码包装在一个if语句中来解决这个问题,该语句检查新时间是否等于已存储在Firebase中的值,如果为真,它什么都不做,但如果不是,那么它应该更新为当前时间。

英文:

I finally found the solution to my problem and i feel i should post it here because this issue doesn't seem to have any solution online.

So what is happening here is that i did not add a check for a situation that needs to be true before timestamp will be updated, there by causing the endless loop since it doesn't know when to stop.

So I resolved it by simply wrapping this code

                mRootRef.child("Chat").child(mCurrentUserId).child(mChatUser).child("timestamp").setValue(ServerValue.TIMESTAMP);

in an if statement that checks if the new time is equal to the value already stored in firebase, if it is true it dose nothing but if it is not only then it should update to current time.

huangapple
  • 本文由 发表于 2020年4月7日 03:38:20
  • 转载请务必保留本文链接:https://go.coder-hub.com/61067645.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定