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.
Creating a Database
In DBFlow, creating a database is as simple as only a few lines of code. DBFlow supports any number of databases, however individual tables and other related files can only be associated with one database.
The default name of the database is the class name. To change it, read here.
Writing this file generates (by default) a AppDatabaseAppDatabase_Database.java
file, which contains tables, views, and more all tied to a specific database. This class is automatically placed into the main GeneratedDatabaseHolder
, which holds potentially many databases and global TypeConverter
. The name, AppDatabaseAppDatabase_Database.java
, is generated via {DatabaseClassName}{DatabaseFileName}_Database
To learn more about what you can configure in a database, read here
Initialize FlowManager
DBFlow currently needs an instance of Context
in order to use it for a few features such as reading from assets, content observing, and generating ContentProvider
.
Initialize in your Application
subclass. You can also initialize it from other Context
but we always grab the Application
Context
(this is done only once).
By default without passing in a DatabaseConfig
, we construct an AndroidSQLiteOpenHelper
database instance. To learn more about what you can configure in a database, read here, including providing own database instances.
Finally, add the custom Application
definition to the manifest (with the name that you chose for the class):
A database within DBFlow is only initialized once you call database<SomeDatabase>()
. If you don't want this behavior or prefer it to happen immediately, modify your FlowConfig
:
Each DBFlowDatabase
contains a TransactionManager
, which runs any async
transaction on a queue / dispatch system. If you do not like the built-in DefaultTransactionManager
, or just want to roll your own existing system:
You can define different kinds for each database. To read more on transactions and subclassing BaseTransactionManager
go here
Create Models
Creating models are as simple as defining the model class, and adding the @Table
annotation. To read more on this, read here.
For now: Models must provide a default, parameterless constructor. Also, all fields must be mutable (currently, we hope to evolve this requirement in the future). An example:
Build Your DAO
Set up a DAO (Data Access Object) to help you interact with your database. Using dependency injection and service locating, we can build better, highly testable code. While not required to use DBFlow, it is highly recommended utilize this approach.
With kotlin, we can utilize it in a powerful way:
DBFlow uses expressive builders to represent and translate to the SQLite language.
We can represent the query above in SQLite:
Wherever we perform dependency injection we supply the instance:
Then in our ViewModel
, we can inject it via the constructor and utilize it in our queries:
We support many kinds of complex and complicated queries using the builder language. To read more about this, see the wrapper language docs
There is much more you can do in DBFlow. Read through the other docs to get a sense of the library.
Last updated