casraf
casraf

Reputation: 21694

AsyncTask only works on debug?

Something odd is happening. I have an AsyncTask running so that I don't fetch DB data on the UI Thread. Here's my task:

ProductListActivity.java:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_product_list);
    if (savedInstanceState == null) {
        new QueryProductsTask(this).execute();
    }
}

public void populateProductList(Cursor products) {
    ProductListAdapter productListAdapter =
            new ProductListAdapter(this, R.layout.product_list_row, products);
    ListView listView = (ListView) findViewById(android.R.id.list);
    listView.setAdapter(productListAdapter);
}

QueryProductsTask.java:

protected Cursor doInBackground(Void... params) {
    android.os.Debug.waitForDebugger();
    Log.v("BagIt.AsyncTask", "doInBackground running");
    // Fetches info from database:
    return new Products(activity.getBaseContext()).select(null, null, null, null);
}

protected void onPostExecute(Cursor c) {
    android.os.Debug.waitForDebugger();
    Log.v("BagIt.AsyncTask", "onPostExecute running");
    // Populates ListView through a custom adapter
    activity.populateProductList(c);
}

This code works when I'm in debug mode. There's no fault with the Adapter, or the Db class, as far as I'm aware for now. But when I open the app on my phone, or hit "Run" and not "Debug", it doesn't work. Even if I'm on debug without any breakpoints, it runs, but not outside debug mode.

Here's my logcat:

Debug Mode

10-21 23:35:05.988  15665-15665/com.chenasraf.bagit W/Xposed﹕ Package name for /data/app/com.ugglynoodle.overflowmod-1.apk had to be retrieved via parser
10-21 23:35:06.258  15665-15665/com.chenasraf.bagit W/ActivityThread﹕ Application com.chenasraf.bagit is waiting for the debugger on port 8100...
10-21 23:35:06.328  15665-15665/com.chenasraf.bagit I/System.out﹕ Sending WAIT chunk
10-21 23:35:06.328  15665-15671/com.chenasraf.bagit I/dalvikvm﹕ Debugger is active
10-21 23:35:06.528  15665-15665/com.chenasraf.bagit I/System.out﹕ Debugger has connected
10-21 23:35:06.528  15665-15665/com.chenasraf.bagit I/System.out﹕ waiting for debugger to settle...
10-21 23:35:06.728  15665-15665/com.chenasraf.bagit I/System.out﹕ waiting for debugger to settle...
10-21 23:35:06.929  15665-15665/com.chenasraf.bagit I/System.out﹕ waiting for debugger to settle...
10-21 23:35:07.129  15665-15665/com.chenasraf.bagit I/System.out﹕ waiting for debugger to settle...
10-21 23:35:07.329  15665-15665/com.chenasraf.bagit I/System.out﹕ waiting for debugger to settle...
10-21 23:35:07.529  15665-15665/com.chenasraf.bagit I/System.out﹕ waiting for debugger to settle...
10-21 23:35:07.729  15665-15665/com.chenasraf.bagit I/System.out﹕ waiting for debugger to settle...
10-21 23:35:07.930  15665-15665/com.chenasraf.bagit I/System.out﹕ waiting for debugger to settle...
10-21 23:35:08.130  15665-15665/com.chenasraf.bagit I/System.out﹕ waiting for debugger to settle...
10-21 23:35:08.330  15665-15665/com.chenasraf.bagit I/System.out﹕ debugger has settled (1400)
10-21 23:35:08.510  15665-15847/com.chenasraf.bagit V/BagIt.AsyncTask﹕ doInBackground running
10-21 23:35:08.630  15665-15665/com.chenasraf.bagit I/Adreno-EGL﹕ <qeglDrvAPI_eglInitialize:320>: EGL 1.4 QUALCOMM Build: I0404c4692afb8623f95c43aeb6d5e13ed4b30ddbDate: 11/06/13
10-21 23:35:08.731  15665-15665/com.chenasraf.bagit D/OpenGLRenderer﹕ Enabling debug mode 0
10-21 23:35:08.881  15665-15665/com.chenasraf.bagit V/BagIt.AsyncTask﹕ onPostExecute running
10-21 23:35:09.101  15665-15665/com.chenasraf.bagit W/InputMethodManager﹕ Ignoring onBind: cur seq=4640, given seq=4639

As you can see the Log.v()s are working properly (last few lines)

Run Mode

10-21 23:30:29.545  15022-15022/com.chenasraf.bagit W/Xposed﹕ Package name for /data/app/com.ugglynoodle.overflowmod-1.apk had to be retrieved via parser
10-21 23:30:29.585  15022-15043/com.chenasraf.bagit I/System.out﹕ Sending WAIT chunk
10-21 23:30:29.625  15022-15022/com.chenasraf.bagit I/Adreno-EGL﹕ <qeglDrvAPI_eglInitialize:320>: EGL 1.4 QUALCOMM Build: I0404c4692afb8623f95c43aeb6d5e13ed4b30ddbDate: 11/06/13
10-21 23:30:29.655  15022-15022/com.chenasraf.bagit D/OpenGLRenderer﹕ Enabling debug mode 0

Upvotes: 3

Views: 1156

Answers (3)

petey
petey

Reputation: 17150

remove it or add this if block that will wait and log if you are running in debug mode.

protected Cursor doInBackground(Void... params) {
    if (BuildConfig.DEBUG){
        android.os.Debug.waitForDebugger();
        Log.v("BagIt.AsyncTask", "doInBackground running");
    }
    // Fetches info from database:
    return new Products(activity.getBaseContext()).select(null, null, null, null);
}

protected void onPostExecute(Cursor c) {
    if (BuildConfig.DEBUG){
        android.os.Debug.waitForDebugger();
        Log.v("BagIt.AsyncTask", "onPostExecute running");
    }
    // Populates ListView through a custom adapter
    activity.populateProductList(c);
}

You will need to add an import statement to your class, BuildConfig should have the same package name as your app or library module

Upvotes: 1

codeMagic
codeMagic

Reputation: 44571

From the docs

Wait until a debugger attaches.

Removing that line will allow it to run when no debugger is attached.

Upvotes: 5

Paul Lammertsma
Paul Lammertsma

Reputation: 38272

Your code is explicitly waiting for a debugger to attach. Since you are not running your code in debug mode, waitForDebugger() never returns.

The documentation for waitForDebugger() reads:

Wait until a debugger attaches. As soon as the debugger attaches, this returns

Simply put, remove that logic. Starting your app in debug mode will wait for the debugger to attach automatically.

Upvotes: 3

Related Questions