I am trying to upload a custom object to Firebase Firestore, but the Firestore object should contain a download image url. The idea I have to do this is to upload the image first wait for that to complete, get a reference to the download url, update my custom class, then upload this to Firestore. I would then want to notify my view when this last upload(upload to Firestore) is completed. I was thinking of doing this by returning a task. Here is what I have for upload image:
ref.putFile(file).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
@Override
public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
ref.getDownloadUrl().addOnSuccessListener(new OnSuccessListener<Uri>() {
@Override
public void onSuccess(Uri uri) {
//use uri to update object and then upload to firestore
mObject.setImageUri(uri)
uploadToFirestore(myObject);
}
Log.d(TAG, "onSuccess: uri= "+ uri.toString());
}
});
}
});`
I am not sure how to return the final task of Firstore upload as it is done within the task of image upload. My goal is to listen for when the uploadToFireStore() is completed and then inform my view. Any suggestions would be appreciated. Thanks!
Edit: I have structure like so- View has gets info from user passes it to viewmodel as a custom object. View model then calls db to perform upload. my idea is to return a task from db to viewmodel which will return said task to view. View checks for completion and then stops loader. Is this a good way to think about this?