-
Notifications
You must be signed in to change notification settings - Fork 244
Android
Android support is at the core of this project. requery is the most feature complete ORM available for Android that is also performant. The requery-android project provides classes specific to using Android's SQLite database within the requery core library as well as useful UI adapters/classes. requery works on Android API level 15 and later.
Requirements:
- Android Studio 1.5 or later
- Java JDK 8
- Add the apt gradle plug-in to your build as described here
- Add the requery dependencies to your app build.gradle file:
dependencies {
compile 'io.requery:requery:<latestVersion>'
compile 'io.requery:requery-android:<latestVersion>' // for android
apt 'io.requery:requery-processor:<latestVersion>' // use an APT plugin
}
Replace <latestVersion>
with latest released version.
If you use lint you may need to disable the InvalidPackage error. The core library contains references to classes not available on android however these would never be executed on android so it is safe.
android {
lintOptions {
disable 'InvalidPackage'
}
}
DatabaseSource
is the Android specific connection source for use with EntityDataStore which is the core entity management API of requery. DatabaseSource
extends SQLiteOpenHelper and using the requery API automatically creates tables from the EntityModel provided. It also provides basic upgrade support by adding missing columns and tables on a upgrade. Every time you change the model be sure to increment the schemaVersion passed to DatabaseSource
. If you need to perform more complex migrations just override onUpgrade and perform the necessary migration steps.
SQLCipher is a popular extension to SQLite that encrypts the entire database. You can use requery with SQLCipher by using io.requery.android.sqlcipher.SqlCipherDatabaseSource
instead of DatabaseSource
when creating the store and providing a database password. Also be sure to include the SQLCipher dependency in your gradle file
compile 'net.zetetic:android-database-sqlcipher:<version>'
See the project page for more information. This is an embedded version of SQLite you can include with your application instead of the default system version. To use it include the SQLite library dependency and use io.requery.android.sqlitex.SqlitexDatabaseSource
instead of DatabaseSource
when creating the database store.
You can take advantage of the new databinding library from Google in your entities. Simply extend Observable and provide @Bindable on bindable properties.
@Entity
public interface Person extends Observable, Parcelable, Persistable {
@Key @Generated
int getId();
@Bindable // android databinding annotation
String getName();
...
With binding enabled you can automatically update your UI when the underlying database entity is changed or updated.
A typical use of a query Result instance is to use it's items to populate a list. For this QueryRecyclerAdapter
can be used to populate a RecyclerView with query result data.
For async operations we recommend using RxJava. requery provides a complete API that encapsulates all common database operations (insert/update/delete/refresh) using the Rx Single API. See here for more information.
See the requery-android/example project for a working example.
Use databaseSource.setLoggingEnabled(true);
to enable sql statements and entity operations output to the ADB log.