Getting Started
This section describes how Models and tables are constructed via DBFlow. first let's describe how to get a database up and running.
Last updated
Was this helpful?
Was this helpful?
<application
android:name="{packageName}.ExampleApplication"
...>
</application>override fun onCreate() {
super.onCreate()
FlowManager.init(this) {
openDatabasesOnInit(true)
})
}FlowManager.init(this) {
database<AppDatabase> {
transactionManagerCreator { db -> CustomTransactionManager(db) }
}
}@Table(database = TestDatabase::class)
class Currency(@PrimaryKey(autoincrement = true) var id: Long = 0,
@Unique var symbol: String? = null,
var shortName: String? = null,
@Unique var name: String = "") // nullability of fields are respected. We will not assign a null value to this field./**
* Create this class in your own database module.
*/
interface DBProvider<out T: DBFlowDatabase> {
val database: T
}
interface CurrencyDAO : DBProvider<AppDatabase> {
/**
* Utilize coroutines package
*/
fun coroutineRetrieveUSD(): Deferred<MutableList<Currency>> =
database.beginTransactionAsync {
(select from Currency::class
where (Currency_Table.symbol eq "$")).queryList(it)
}.defer()
/**
* Utilize RXJava2 package.
* Also can use asMaybe(), or asFlowable() (to register for changes and continue listening)
*/
fun rxRetrieveUSD(): Single<MutableList<Currency>> =
database.beginTransactionAsync {
(select from Currency::class
where (Currency_Table.symbol eq "$"))
.queryList(it)
}.asSingle()
/**
* Utilize Vanilla Transactions.
*/
fun retrieveUSD(): Transaction.Builder<MutableList<Currency>> =
database.beginTransactionAsync {
(select from Currency::class
where (Currency_Table.symbol eq "$"))
.queryList(it)
}
/**
* Utilize Paging Library from paging artifact.
*/
fun pagingRetrieveUSD(): QueryDataSource.Factory<Currency, Where<Currency>> = (select from Currency::class
where (Currency_Table.symbol eq "$"))
.toDataSourceFactory(database)
}SELECT * FROM Currency WHERE symbol='$';fun provideCurrencyDAO(db: AppDatabase) = object : CurrencyDAO {
override val database: AppDatabase = db
}class SampleViewModel(private currencyDAO: CurrencyDAO)