I am having trouble in executing any async task from inside the onCompleted function of onCompletedListener.
I want to execute 2 things
- Upload the pic to firebase storage and obtain a downloadable link. (Done)
- Update that downloadable link in my user table in firestore. (Not getting executed)
My approach was to obtain the downloadString in the onCompleteListener of the UploadTask. And then pass it as input to another Worker thread to update my user table. The new worker thread is launched from the onCompleteListener of the first task.
Here's a snippet of my code.
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(); //TODO catch it later
}
return finalPicStorageReference.getDownloadUrl();
}
}).addOnCompleteListener(new OnCompleteListener<Uri>() {
@Override
public void onComplete(@NonNull Task<Uri> task) {
if (task.isSuccessful()) {
Uri downloadUri = task.getResult();
Log.d(TAG, "onComplete: Successfully Uploaded");
if (downloadUri != null) {
String picDownloadString = downloadUri.toString();
Constraints.Builder builder = new Constraints.Builder();
Data.Builder data = new Data.Builder();
if (profileBoolean) {
data.putBoolean("profileBoolean", true);
} else if (coverBoolean) {
data.putBoolean("coverBoolean", true);
}
data.putString("picDownloadString", picDownloadString);
/*OneTimeWorkRequest updatePicEntryAtDB = new OneTimeWorkRequest
.Builder(UpdateUserDatabaseWorker.class)
.addTag("Updating pic in database")
.setInputData(data.build())
.build();
WorkManager.getInstance(getApplicationContext()).beginUniqueWork("picUpdationInDB", ExistingWorkPolicy.APPEND,
updatePicEntryAtDB).enqueue();
*/results[0] = Result.success();
}
} else {
results[0] = Result.failure();
}
}
});
The code uploads the pic perfectly when it is commented like this. But on removing the comment to allow updating of the user table, the code stops working altogether. That is, the pic doesn't get uploaded too.