updateChildren不更新图像数组,其覆盖现有的array(url)数据firebase android

时间:2018-09-25 07:54:39

标签: android firebase-realtime-database

updateChildren不更新图像数组,其覆盖现有的array(url)数据firebase android。我想在Firebase中的现有URL数组中添加更多图像。  这是我的firebase结构

  Blog--
     pushid--
       "name": xxxx
        "url"
             -- 0: "first image"
                1: "second image"

我想更新(将更多图像添加到现有的url数组。我使用updatechildren(),但是它覆盖了现有图像

for (String photo : selectedImages) {
                    blogimages = new ArrayList<>();
                    Uri file = Uri.fromFile(new File(photo));
                    Bitmap bitmap = null;
                    try {
                        bitmap = decodeSampledBitmapFromUri(file, mMaxDimension, mMaxDimension);


                        StorageReference photoRef = mstorageReference.child("images/" + file.getLastPathSegment());
                        uploadTask = photoRef.putBytes(byteArray);

                        uploadTask.addOnProgressListener(new OnProgressListener<UploadTask.TaskSnapshot>() {
                            @Override
                            public void onProgress(UploadTask.TaskSnapshot taskSnapshot) {



                            }
                        }).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
                            @Override
                            public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
                                Uri downloadUrl = taskSnapshot.getDownloadUrl();
                                blogimages.add(downloadUrl.toString());

                                Map<String, Object> childUpdates = new HashMap<>();
                                childUpdates.put("url", blogimages);

                                HashMap<String, Object> params = new HashMap<>();

                                params.put("url", blogimages);
                                uploads.child(post_key).child("url").updateChildren(params).addOnCompleteListener(new OnCompleteListener<Void>() {
                                    @Override
                                    public void onComplete(@NonNull Task<Void> task) {
                                        if (task.isSuccessful()) {
                                            Toast.makeText(UpdatePost.this, "Post updated..", Toast.LENGTH_LONG).show();
                                            MainActivity.loaded = true;
                                            Intent i = new Intent(UpdatePost.this, MainActivity.class);
                                            i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                                            startActivity(i);
                                        }
                                    }
                                }).addOnFailureListener(new OnFailureListener() {
                                    @Override
                                    public void onFailure(@NonNull Exception e) {
                                        Toast.makeText(UpdatePost.this, "Failed to post the blog.. Try again later", Toast.LENGTH_LONG).show();
                                    }
                                });

                            }
                        });
                    } catch (FileNotFoundException e) {
                        Log.e(TAG, "Can't find file to resize: " + e.getMessage());
                        FirebaseCrash.report(e);
                    } catch (IOException e) {
                        Log.e(TAG, "Error occurred during resize: " + e.getMessage());
                        FirebaseCrash.report(e);
                    }
                }

1 个答案:

答案 0 :(得分:0)

这不是updateChildren()的工作方式。 updateChildren()将完全覆盖您指定的所有子字段的内容。它根本不会“推送”数组元素,也不会执行任何其他类似数组的操作。如果要追加到数组,则必须从数据库中读取数组的内容,然后添加元素,然后将其写回。

请注意,Firestore的不同之处在于,它具有arrayUnion操作,可将非唯一数组元素附加到数组类型字段。