简体   繁体   English

Firebase:在我说之前,不要向所有听众发送消息

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

I have a system of chat in my Android application, using Firebase realtime Database. 我的Android应用程序中有一个使用Firebase实时数据库的聊天系统。 I added the option to add an image to a message, using Firebase Storage. 我添加了使用Firebase存储向邮件添加图像的选项。 The problem is, that after I just uploaded the data about the message, and still not the image, a ValueEventListener is called and updates the messages in the chat, when the image is still not fully uploaded. 问题是,在我刚刚上载有关消息的数据(仍然不是图像)之后,当图像仍未完全上载时,将调用ValueEventListener并更新聊天中的消息。 There is any way to tell Firebase when to send an update to all the listeners? 有什么方法可以告诉Firebase何时向所有侦听器发送更新?

This is my code for uploading to 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) { }
        });

Is there is any function to do that?? 有任何功能可以做到这一点吗?

Listeners always fire immediately after any changes are see at the location of the query. 在查询位置看到任何更改后,侦听器总是立即触发。 There is no way to tell the SDK to delay the callback. 无法告诉SDK延迟回调。 Your code should probably complete the upload first before writing anything to the database. 您的代码可能应该先完成上载,然后再将任何内容写入数据库。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM