Reputation: 2623
Here's my question: I have an app that needs to make a write operation, on an SQLite database, one time per second, it is better to use an AsyncTask to write data on this database or not?
public void insertData(Data data) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues RecordValues = new ContentValues();
RecordValues.put("uid", data.getUid());
RecordValues.put("id_usr", data.getUserId());
RecordValues.put("id_route", data.getIdRoute());
RecordValues.put("lat", data.getLatitude());
RecordValues.put("lng", data.getLongitude());
RecordValues.put("timestamp", data.getTime());
RecordValues.put("privacy", data.getTime());
db.insert("DBNAME", null, RecordValues);
db.close();
}
The DB is implemented by using a SqLiteClass
.
The app makes some heavy tasks, working with live data, web-socket, google map and so on, so I want to optimize on this point.
I don't know if starts an asyncTask one time per second is better or not, i can make a mistake falling in error so we can speak about that.
Thanks in advance.
Upvotes: 2
Views: 211
Reputation: 1511
If I were you I would use a dedicated HandlerThread for that. Android Performance video series by Google recommends to use it, when you have a significant amount of operations to perform and they can be split into parts. You could do something like this:
//make sure you have one instance of this HandlerThread
private HandlerThread updateThread = acquireThisThread();
private Handler updateHandler;
private void someInitializingFunction() {
updateThread.start()
updateHandler = new Handler(updateThread.getLooper())
}
private void onEachSecondCallback() {
updateHandler.post(new Runnable() {
@Override
public void run() {
//your insertingDataFunction
insertData(getDataSomehow());
}
});
}
private void someDeinitializingFunction() {
updateThread.quit();
}
And here you have a link to the performance video. It uses totaly different example ,but if you base on that you will get why HandlerThread is cool here. https://www.youtube.com/watch?v=adPLIAnx9og
Upvotes: 1
Reputation: 94
Do not insert data in main thread.if your data is big it might be hung up MainThread. its better to insert in background thread useing AsyncTask,Handler,Third Party library RxJava. and must use beginTransaction().
public void dbInsert()
{
db.beginTransaction();
//do your insertion.
db.setTransactionSuccessful();
db.endTransaction();
}
Upvotes: 1
Reputation: 24211
Yes, this is recommended to have the database operation in a background thread. You might consider using an AsyncTask
or Handler
to handle the DB operations in a background thread instead of putting this in the main UI thread.
You might be also curious about getting the update on your database on changing or inserting an item to your database table. You might consider, using the content observer to get notified about the content changes in your database table. Please check the LoaderCallbacks
functions.
I have created a project in Github here to demonstrate the database operations using LoaderCallbacks
. However, I have put all the database operations in the UI thread. It is better to handle them inside another thread using AsyncTask
or Handler
.
Upvotes: 1