DBFlow icon indicating copy to clipboard operation
DBFlow copied to clipboard

Batch Transaction Help

Open brwskitime opened this issue 10 years ago • 1 comments

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.

brwskitime avatar Mar 24 '16 21:03 brwskitime

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.

agrosner avatar Apr 21 '16 20:04 agrosner