简体   繁体   中英

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. I added the option to add an image to a message, using Firebase Storage. 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. There is any way to tell Firebase when to send an update to all the listeners?

This is my code for uploading to 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. Your code should probably complete the upload first before writing anything to the database.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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