Reputation: 167
I am storing some data into the firebase firestore and using onComplete listener on the operation. I am Storing the ID which I generated, in a String and using it to perform further operations.
ProductRef = db.collection("Sellers").document(CityName).collection(Uid).document();
ProductID =db.collection("Sellers").document(CityName).collection(Uid).document().getId();
I am then using that generated Id to as a field in Cloud Storage and adding some images inside it and after it using the same ID I am storing the URLs of these images in the firestore but what happening is a different Id is generated in between and details are stored inside one Id and ImageUrls inside another.
My code
private void UploadDetails() {
ProductRef = db.collection("Sellers").document(CityName).collection(Uid).document();
ProductID =db.collection("Sellers").document(CityName).collection(Uid).document().getId();
HashMap<String, Object> map = new HashMap<>();
map.put("City_Name", CityName);
ProductRef.set(map, SetOptions.merge()).addOnCompleteListener(new OnCompleteListener<Void>() {
@Override
public void onComplete(@NonNull Task<Void> task) {
UploadingImage();
UploadingThumbnailImage();
}
});
}
private void UploadingImage() {
resized = Bitmap.createScaledBitmap(bitmap, 800, 800, true);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
resized.compress(Bitmap.CompressFormat.JPEG, 70, baos);
byte[] uploadbaos = baos.toByteArray();
ProductRef = db.collection("Sellers").document(CityName).collection(Uid).document(ProductID);
fileReference = storageReference.child(ProductID).child("1" + ProductID + ".jpg");
uploadTask = fileReference.putBytes(uploadbaos);
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();
} else if (task.isSuccessful()) {
Toast.makeText(Upload_New_Product.this, "Uploaded Successfully", Toast.LENGTH_SHORT).show();
//mProgressBar.setVisibility(View.INVISIBLE);
}
return fileReference.getDownloadUrl();
}
}).addOnCompleteListener(new OnCompleteListener<Uri>() {
@Override
public void onComplete(@NonNull Task<Uri> task) {
if (task.isSuccessful()) {
Uri downloadUri = task.getResult();
String mUri = downloadUri.toString();
// ProductRef=db.collection("Sellers").document().collection(ProductType);
ProductName = Product_Name_EditText.getText().toString();
// ProductRef = db.collection("Sellers").document(CityName).collection(Uid).document(ProductID);
//reference = FirebaseDatabase.getInstance().getReference("Users").child(fuser.getUid());
HashMap<String, Object> map = new HashMap<>();
map.put("imageURL", mUri);
//reference.updateChildren(map);
ProductRef.set(map, SetOptions.merge());
//pd.dismiss();
} else {
Toast.makeText(Upload_New_Product.this, "Failed!", Toast.LENGTH_SHORT).show();
//pd.dismiss();
}
}
}).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
Toast.makeText(Upload_New_Product.this, e.getMessage(), Toast.LENGTH_SHORT).show();
//pd.dismiss();
}
});
}
Upvotes: 0
Views: 153
Reputation: 317948
Every time you call document()
with no parameters, you're going to get a different random document ID. Your code here is calling document()
twice:
ProductRef = db.collection("Sellers").document(CityName).collection(Uid).document();
ProductID = db.collection("Sellers").document(CityName).collection(Uid).document().getId();
If you want a single random ID to reuse, just call it once:
ProductRef = db.collection("Sellers").document(CityName).collection(Uid).document();
ProductID = ProductRef.getId();
Upvotes: 1