MDP
MDP

Reputation: 4267

Should I excute Insert, Delete, Update on my own SQLite DB inside AsyncTaskLoader

I read many posts telling long operations (like reading DB) should not be excuted on the main Thread.

So far I used CursorLoader (for "reading" ContentResolver) andAsynkTaskLoader(for "reading" my own SQLite DB).

I know to perform Insert, Delete and Update "on" ContentResolver I can use AsyncQueryHandler.

My question is: what's the best way to execute Insert, Delete and Update on my own SQLite db??

Should I simply execute those operations inside the method loadInBackground() of AsyncTaskLoader?

Thank you

Upvotes: 2

Views: 103

Answers (2)

Jitesh Mohite
Jitesh Mohite

Reputation: 34210

You can use background handler thread for your purpose.

private Handler mHandler = null;  // global variable
private HandlerThread mHandlerThread = null; // global variable

// Create instance inside onCreate() or another place inside your code so it would be called one's. Also you can make it singleton if required but it's not the right way.

    mHandlerThread = new HandlerThread("HandlerThread");
    mHandlerThread.start();
    mHandler = new Handler(mHandlerThread.getLooper());

// perform background operation like :

handler.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                       // Do background work here.

                   }
                }, 2000);

Upvotes: 1

Bhuvanesh BS
Bhuvanesh BS

Reputation: 13617

If your queries are simple and it won't make much time to execute then simply you can use it in main thread itself.

If you're doing big data process then you have two options.

  1. AsyncTask:

If you want to update your UI after an operation, you can use AsyncTask to do the process in a background thread and pass the result into the main thread. for more info about AsyncTaksk click here.

2.Thread

If there is no purpose of updating UI after the operation you can simply use Thread to do it.

Upvotes: 1

Related Questions