0

I have the following class which is responsible to fetch non synced receipts from the receipts table and upload them to the server, the following function as of now just iterates through the cursor result set:

public class MidnightUpload {


    public static void checkLocalAndUpload(final Context ctx) {

        Cursor cursor = DatabaseHandler
                .getInstance(ctx)
                .getReadableDatabase()
                .query(Receipt.TABLE_NAME, Receipt.FIELDS,
                        Receipt.WEB_RECEIPT_ID + " IS ?", new String[]{"dummy"},
                        null, null,
                        null, null);

        if (cursor != null && cursor.moveToFirst()) {

            do {
                Log.d("_id", cursor.getString(cursor.getColumnIndexOrThrow("_id")));
                Log.d("receipt_id", cursor.getString(cursor.getColumnIndexOrThrow("receipt_id")));
                Log.d("web_receipt_id", cursor.getString(cursor.getColumnIndexOrThrow("web_receipt_id")));
                Log.d("receipt_name", cursor.getString(cursor.getColumnIndexOrThrow("receipt_name")));
              //  Log.d("image", cursor.getString(cursor.getColumnIndexOrThrow("image")));
                Log.d("date_added", cursor.getString(cursor.getColumnIndexOrThrow("date_added")));
                Log.d("status", cursor.getString(cursor.getColumnIndexOrThrow("status")));
                Log.d("currency", cursor.getString(cursor.getColumnIndexOrThrow("currency")));
                Log.d("category", cursor.getString(cursor.getColumnIndexOrThrow("category")));
                Log.d("sub_category", cursor.getString(cursor.getColumnIndexOrThrow("sub_category")));
                Log.d("payment", cursor.getString(cursor.getColumnIndexOrThrow("payment")));
                Log.d("invoice", cursor.getString(cursor.getColumnIndexOrThrow("invoice")));
                Log.d("custom_field", cursor.getString(cursor.getColumnIndexOrThrow("custom_field")));
                Log.d("organization", cursor.getString(cursor.getColumnIndexOrThrow("organization")));
                Log.d("person", cursor.getString(cursor.getColumnIndexOrThrow("person")));

            } while (cursor.moveToNext());
        }


    }
}

I am aware that I can start multiple Async Tasks using:

 asyncTask.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, params);

The above method I am planning to call from an IntentService. So here are my confusions:

1) Will the do while loop wait till control has returned from asyncTask for the next iteration?

2)Will using and spawning multiple threads within an intentService disrupt my program?

3) Am I better off using Runnable r = new Runnable() than AsyncTask - as I dont intend any UI operation?

User3
  • 2,465
  • 8
  • 41
  • 84
  • Your confusions confused me. Why wait when you want to do things asynchronously (`AsyncTask`)? Use either `IntentService` or `AsyncTask`, not both together. I believe `IntentService` works better for your case. – LightYearsBehind Jul 08 '15 at 10:19
  • I dont want to wait, that is where you are confused, I want to loop the do while say first iteration - spawn a upload - dont wait for upload to return - loop second time - spawn to upload - dont wait for upload to return - spawn as many times as loops. – User3 Jul 08 '15 at 10:28
  • `AsyncTask` doesn't wait, nor does `IntentService`. I suggest you to use `IntentService`, move your `do...while` loop there together with the upload code. – LightYearsBehind Jul 08 '15 at 10:32

0 Answers0