How to combine several greenDAO Tx operations in one transaction?

My application uses the remote REST API and populates the local db using greenDao. I have a service with the AsyncTask class:

@Override protected Void doInBackground(Void... params) { insert100RowsIntheFirstTable(); insert100RowsIntheSecondTable(); } 

Inside each insert method, I have insertOrReplaceInTx, which I use primarily to increase performance.

I need to discard the results if any of the methods cannot get the data. This must be done through the same transaction.

I wonder if it is correct to surround my calls with the insert method with mDaoSession.callInTx(callable) , having insertOrReplaceInTx inside the methods. I'm right?

In addition, how can I refuse a transaction in case of an exception - is this done automatically with greenDao?

+6
source share
1 answer

Yes, use callInTx if your code can throw an exception (if you cannot consider runInTx as well. The Android SQLite API will take care of these "nested" transactions.

After all, callInTx are just some handy lines if you look at the source code:

 public <V> V callInTx(Callable<V> callable) throws Exception { db.beginTransaction(); try { V result = callable.call(); db.setTransactionSuccessful(); return result; } finally { db.endTransaction(); } } 
+9
source

All Articles