一次单击即可更新和删除两个不同集合中的文档

时间:2019-05-14 15:22:03

标签: android firebase google-cloud-firestore

我正在创建一个类似于olx的应用程序,如果您上传广告,它将在两个不同的集合中发布,分别是“我的广告”,其中每个登录用户的广告均按照其ID发布,并在“探索”集合中发布。为所有用户发布。因此,如果我要更新或删除广告,则应从两个收藏夹中将其删除!图片网址:https://imgur.com/a/HCKfGBb

字符串uid = user.getUid();                 SellingDetails.put(“ uid”,uid);

            final CollectionReference reference = exploreAdDB.collection("cities/" + city + "/" + category);
            final CollectionReference myAdDocRef = myAdDB.collection("users/ads/"+uid);
            if (imageUri != null) {
                final StorageReference fileReference = mStorage.child(System.currentTimeMillis()
                        + "." + getFileExtension(imageUri));
                UploadTask uploadTask = fileReference.putFile(imageUri);
                Task<Uri> urlTask = uploadTask.continueWithTask(new Continuation<UploadTask.TaskSnapshot, Task<Uri>>() {
                    @Override
                    public Task<Uri> then(@NonNull Task<UploadTask.TaskSnapshot> task) throws Exception {
                        if (!task.isSuccessful()) {
                            throw task.getException();
                        }
                        return fileReference.getDownloadUrl();
                    }
                }).addOnCompleteListener(new OnCompleteListener<Uri>() {
                    @Override
                    public void onComplete(@NonNull Task<Uri> task) {
                        if (task.isSuccessful()) {
                            Uri downloadUri = task.getResult();
                            if(downloadUri!=null)
                                SellingDetails.put("imageUrl",downloadUri.toString());
                            reference.add(SellingDetails).addOnSuccessListener(new OnSuccessListener<DocumentReference>() {
                                @Override
                                public void onSuccess(DocumentReference documentReference) {
                                    Toast.makeText(GetLocationActivity.this, "Service Uploaded in explore db ", Toast.LENGTH_SHORT).show();
                                    pgAd.setVisibility(View.INVISIBLE);
                                    Intent intent = new Intent(GetLocationActivity.this, MainActivity.class);
                                    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
                                    startActivity(intent);
                                    Toast.makeText(GetLocationActivity.this, "Ad posted Successfully", Toast.LENGTH_LONG).show();
                                }
                            }).addOnFailureListener(new OnFailureListener() {
                                @Override
                                public void onFailure(@NonNull Exception e) {
                                    Toast.makeText(GetLocationActivity.this, "Failed adding data into explore db", Toast.LENGTH_SHORT).show();
                                    pgAd.setVisibility(View.INVISIBLE);
                                }
                            });
                            myAdDocRef.add(SellingDetails).addOnSuccessListener(new OnSuccessListener<DocumentReference>() {
                                @Override
                                public void onSuccess(DocumentReference documentReference) {
                                    Toast.makeText(GetLocationActivity.this, "Data Also added to myAds db", Toast.LENGTH_SHORT).show();
                                }
                            })
                                    .addOnFailureListener(new OnFailureListener() {
                                        @Override
                                        public void onFailure(@NonNull Exception e) {
                                            Toast.makeText(GetLocationActivity.this, "Data not added into my ads", Toast.LENGTH_SHORT).show();

                                        }
                                    });


                        } else {
                            // Handle failures
                            Toast.makeText(GetLocationActivity.this, "Failed adding data", Toast.LENGTH_SHORT).show();
                        }
                    }
                });



            }

0 个答案:

没有答案