• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps Troubles with AsyncTask

Hi, I can't use my main thread for going online it seems. I have to check if my user is already logged in, and this is done like this:

HTML:
public boolean isUserLoggedIn(Context context){
        DatabaseHandler db = new DatabaseHandler(context);
        int count = db.getRowCount();
        if(count > 0){
            // user logged in
            return true;
        }
        return false;
    }
How do i use the AsyncTask on this, when it returns boolean?
DatabaseHandler just checks if theres a row with information and returns 1 if so.
 
1) If there's a row returned, the user is logged in, right?

2) It returns True, not 1. (Don't mix Boolean and numeric thinking, or one day you'll have a bug you can't find - because you're looking for the wrong bug.)

3) I think you can shorten your code to

int count = db.GetRowCount();
return (count > 0);

(Doing things like that all over are the difference between a large app and a small app that have the same code. And smaller code runs on older phones.)

(No clue about AsyncTask - I don't write Android apps.)
 
AsyncTask needs to die. It's a horrible and dangerous abstraction.

That said, I still have a few of them in my code for fast, off-main-thread work. (That I should probably remove someday)

But here's how you would do it:


PHP:
AsyncTask<Context, Void, Boolean> task 
        = new AsyncTask<Context, Void, Boolean>() {

    @Override
    protected Boolean doInBackground(Context... params) {
        DatabaseHandler db = 
                new DatabaseHandler(params[0]);
        int count = db.getRowCount();
        if(count > 0) {
            // user logged in
            return true;
        }
        return false;
    }

    @Override
    protected void onPostExecute(Boolean result) {
        //handle result
    }
};

task.execute(context);
//just used PHP code tags for coloring
 
AsyncTask needs to die. It's a horrible and dangerous abstraction.

That said, I still have a few of them in my code for fast, off-main-thread work. (That I should probably remove someday)

But here's how you would do it:


PHP:
AsyncTask<Context, Void, Boolean> task 
        = new AsyncTask<Context, Void, Boolean>() {

    @Override
    protected Boolean doInBackground(Context... params) {
        DatabaseHandler db = 
                new DatabaseHandler(params[0]);
        int count = db.getRowCount();
        if(count > 0) {
            // user logged in
            return true;
        }
        return false;
    }

    @Override
    protected void onPostExecute(Boolean result) {
        //handle result
    }
};

task.execute(context);
//just used PHP code tags for coloring

Thanks for your reply! What's the alternative to AsyncTask? I've given multi-threading some thought - would it be better to just learn my way around that, instead of this? - and isn't AsyncTask also doing that? :-)
 
For something as simple as you are doing here, AsyncTask is actually probably OK. (I was being a bit dramatic).

It's bad, but it is simple. Be sure to check if getActivity() is null in onPostExecute() before manipulating anything if you run the task from a Fragment. Or, if you run from an activity, check anything you manipulate for null before manipulating it.

It does have problems though -- there is no built in callback for error handling, it's easy to leak Context and the thread is stuck in a single thread pool as of Android 4.0. I'd also bet a huge percentage of all the NullPointerExceptions ever created on Android are due to misuse of AsyncTask -- (often when the user rotates the device).

Anyways, if the work you do is going to return quickly, then it's fine -- you wont ever even see most of these problems. But when you get to more complex stuff you might want a more complete and safe solution.

I am personally learning RxJava right now for concurrency and it is fantastic.

Android Loaders are another option for DB loading.

Square has something called Okio for file IO. (I havent used this)

For RESTful HTTP and/or remote image loading I would suggest Volley or Retrofit+Picasso


I would not advise writing your own multi-threading code unless you really want to learn it -- then I would just do it to learn and not put it in production code. It's something that has been solved 1000 times successfully by a lot of libraries. (Don't re-invent the wheel, so to speak).


Other interesting libraries:
- Guava
- Robospice
 
Back
Top Bottom