agrosner / DBFlow

A blazing fast, powerful, and very simple ORM android database library that writes database code for you.
MIT License
4.88k stars 600 forks source link

Different between processInTransactionAsync and beginTransactionAsync #1516

Closed ukevgen closed 6 years ago

ukevgen commented 6 years ago

DBFlow Version:

Description: Hi! Could you explain me next situation: I tried to ways to update my tables: ` @Table(database = CityDatabase::class, name = StopData.TABLE_NAME) class StopData : BaseModel() { companion object { const val TABLE_NAME = "Stops" }

@Index
@Column
@PrimaryKey
@Unique(onUniqueConflict = ConflictAction.REPLACE)
var id = EMPTY_LONG

@Column
var latitude = EMPTY_DOUBLE
@Column
var longitude = EMPTY_DOUBLE
@Column
var name = EMPTY_STRING

@get: OneToMany(methods = arrayOf(OneToMany.Method.ALL))
var stopRoutes: List<StopRoutesData>? by oneToMany { select from StopRoutesData::class where (StopRoutesData_Table.stopId.eq(id)) }
stops: List<StopData> - not empty
            stops.processInTransactionAsync(
                    { stopData, databaseWrapper -> stopData.insert(databaseWrapper) },
                    Transaction.Success { },
                    Transaction.Error { transaction, error ->
                        transaction.cancel()
                    }
            )

and

database<CityDatabase>().beginTransactionAsync(object : ITransaction {
                override fun execute(databaseWrapper: DatabaseWrapper) {
                    stops.fastInsert()
                }
            }).build().execute()

And in the first variant my datas saved successfully,  but second variant didn't save my datas. What I missed? And could you provide fast insert example with relationships ?

douglasalipio commented 6 years ago

@ukevgen did you find the answer?

agrosner commented 6 years ago

processInTransactionAsync is removed in 5.0.0-alpha1. beginTransactionAsync starts a Transaction.Builder but you need to explicitly execute() it.

agrosner commented 6 years ago

stops.fastInsert() creates a FastStoreModelTransaction.Builder class, which does not actually execute the transaction yet. See this doc: https://dbflow.gitbook.io/dbflow/usage2/usage/storingdata#faststoremodeltransaction still valid for 4.2.4, but in 5.0.0-alpha1 its the only way.