Friends,
I would like to know about this error. I am using Firebase in Android. The complete error is:
Fatal Exception: java.util.concurrent.RejectedExecutionException
Task com.google.firebase.a.v@61b9a00 rejected from java.util.concurrent.ThreadPoolExecutor@eacc239[Running, pool size = 2, active threads = 2, queued tasks = 128, completed tasks = 0]
The error is happening on DataChange
inside a Fragment
, who is reading the information to populate the RecyclerView
.
I would like to know why is this happening and how I should avoid this error.
Thank you and sorry, because I know I am not explaining the issue in detail, but I really do not know why this is happening.
This is the code where I realized that it shows the error. It all started when I tried to upload more than 5 pictures at the same time.
if (!pet.isUploaded()) {
File image = new File(pet.getPicPath());
final StorageReference referenceImage = storageRef.child("Adopcion" + "/" + pet.getOwner() + "/" + "Adopcion" + "/" + pet.getName() + pet.getAnimalID());
Uri image2 = Uri.parse("");
if (image.exists()) {
image2 = Uri.fromFile(new File(image.toURI()));
}
UploadTask taskImage = referenceImage.putFile(image2);
taskImage.addOnCompleteListener(new OnCompleteListener<UploadTask.TaskSnapshot>() {
@Override
public void onComplete(@NonNull Task<UploadTask.TaskSnapshot> task) {
if (task.isSuccessful()) {
if (task.getResult().getDownloadUrl() != null) {
pet.setUploaded(true);
pet.setPicPath(task.getResult().getDownloadUrl().toString());
mReference.child(String.valueOf(pet.getAnimalID())).setValue(pet).addOnCompleteListener(new OnCompleteListener<Void>() {
@Override
public void onComplete(@NonNull Task<Void> task) {
if (getActivity() != null) {
File folder = getActivity().getCacheDir();
File myFile = new File(folder, String.valueOf(pet.getAnimalID()) + ".jpg");
if (myFile.exists()) {
myFile.delete();
}
}
}
});
}
}
}
});
}
The issue was happening because the image was being uploaded many times, because once the upload was successfully completed, I changed my pet status isUploaded to true. However, it takes a few seconds to Firebase to make that change, so the image was being uploaded many times.
What I did to fix this issue, is maybe not the best way to solve it, but it works until I can find a better way. I just use an Array to store the pet id, and once the upload was performed I tracked the pet id to know that the image was already uploaded, and then prevent the image from being queued again.
Before that fix, I realized that the image was being uploaded more than 10 times and even more if the image was created offline.
There is a possibility that you are using a buggy version of firebase. After wasting 4 hours searching for a solution and learning about multi-threading in Android AGAIN! The final solution for me was to downgrade to another version.
For me, this version of dependency worked.
// Firebase
implementation 'com.google.firebase:firebase-core:16.0.8'
implementation 'com.google.firebase:firebase-database:16.1.0'
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With