1

I am using com.birbit:android-priority-jobqueue:1.3.5 to send data to server. Here is how.

@Override
public void onRun() throws Throwable {
  MyStuff myStuff = getMyStuffFromContentProvider(mDbId);
  SendAfterPhotosCallback callback = new SendAfterPhotosCallback() {…}
  callback.xDone=false;
  callback.yDone=false;
  callback.zDone=false;
  sendPhotosXToAmazonS3UsingTransferUtility(myStuff,callback);
  sendPhotosYToAmazonS3UsingTransferUtility(myStuff,callback);
  sendPhotosZToAmazonS3UsingTransferUtility(myStuff,callback);
  //callback.sendJson(myStuff);
}

What’s the big deal? sendJson must be called after the other three methods are done with their work. But TransferUtility sends data to S3 asynchronously. So to track that each of these -ToAmazonS3UsingTransferUtility is successful I am using an AtomicInteger to count the completion of each TransferObserver.

public static abstract class SendAfterPhotosCallback{
        static boolean xDone = false;
        static boolean yDone = false;
        static boolean zDone = false;
        abstract public void sendJson(MyStuff myStuff)  throws Throwable;
    }

When each -ToAmazonS3UsingTransferUtility is completely done, it sets its respective flag to true (i.e. callback.xDone=true) and then calls callback.sendJson(myStuff). Inside sendJson I check if all the flags are set to then send the json to server.

Anyway, since each -ToAmazonS3UsingTransferUtility will call callback.sendJson(myStuff) asynchronously, what happens if callback.sendJson(myStuff) throws an exception? Will onRun() of the job queue see it?

This is important because when onRun throws an exception the JobQueue knows to retry later.

To the best of my knowledge onRun is done running when the async calls are made. So how do I fix this problem so the code works well? Is there a way to get TransferUtility to work synchronously perhaps?

Nouvel Travay
  • 6,292
  • 13
  • 40
  • 65

0 Answers0