Reputation: 4267
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
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
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.
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