DBFlow
DBFlow copied to clipboard
Batch Transaction Help
I need to insert a bunch of records in a transaction, but I can't have them all in memory.
This works fine:
public static <T extends BaseTable> void saveInTransaction(List<T> list, TransactionListener<List<T>> listener) {
TransactionManager manager = TransactionManager.getInstance();
manager.addTransaction(new SaveModelTransaction<>(ProcessModelInfo.withModels(list).result(listener)));
}
I need the version of this that operates one at a time then commits at the end. An example in the docs would be great.
I tried this:
DatabaseWrapper db = FlowManager.getDatabase(SNOCRUDatabase.NAME).getWritableDatabase();
db.beginTransaction();
try {
for (Model mymodel : models) {
mymodel.save();
}
db.setTransactionSuccessful();
}
finally {
db.endTransaction();
}
But I get a database locked error. Not happy about that anyway. I'd rather do it the right way.
I would try out the new Transaction apis coming in beta6, in develop currently. (https://github.com/Raizlabs/DBFlow/blob/develop/usage2/StoringData.md). Then new system only accesses the DB object when it needs to when its run and also sychronizes access to the OpenHelper so you shouldn't be getting database access issues.