簡體   English   中英

Firebase:在我說之前,不要向所有聽眾發送消息

[英]Firebase: Don't send message to all the listeners until I say

我的Android應用程序中有一個使用Firebase實時數據庫的聊天系統。 我添加了使用Firebase存儲向郵件添加圖像的選項。 問題是,在我剛剛上載有關消息的數據(仍然不是圖像)之后,當圖像仍未完全上載時,將調用ValueEventListener並更新聊天中的消息。 有什么方法可以告訴Firebase何時向所有偵聽器發送更新?

這是我上傳到Firebase的代碼:

final DatabaseReference postsReference = databaseReference.child(DEBUG.getPostsPath());
        postsReference.child("NumberOfPosts").addListenerForSingleValueEvent(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                int num = dataSnapshot.getValue(Integer.class)+1;

                DatabaseReference newPostReference = postsReference.child("PostNumber" + num);
                newPostReference.child("Username").setValue(firebaseAuth.getCurrentUser().getDisplayName());
                newPostReference.child("Post").setValue(post);

                DatabaseReference idRef = newPostReference.child("id");
                String key = idRef.push().getKey();
                idRef.setValue(key);

                if (imagePath != null) {
                    String id = getUniqueID();

                    FirebaseStorage storage = FirebaseStorage.getInstance();

                    StorageReference ref = storage.getReference();
                    StorageReference imageRef = ref.child(DEBUG.getPostsImagesPath() + "/" + id);

                    imageRef.putBytes(toByteArray(imagePath));
                    newPostReference.child("Image").setValue(id);
                }

                if (father != null) newPostReference.child("Father").setValue(father); //Set the ID of is father

                postsReference.child("NumberOfPosts").setValue(num);
            }

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

有任何功能可以做到這一點嗎?

在查詢位置看到任何更改后,偵聽器總是立即觸發。 無法告訴SDK延遲回調。 您的代碼可能應該先完成上載,然后再將任何內容寫入數據庫。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM