infinum android talks #09 - dbflow orm

Post on 16-Jul-2015

479 Views

Category:

Software

2 Downloads

Preview:

Click to see full reader

TRANSCRIPT

We design and develop great software for mobile and web.

DBFlow ORM library

Željko Plesac Android talks #10

Scope

• Intro

• Basic functions

• Advanced functions

• Benchmark tests

• Conclusion

Intro• debugging SQL errors is not easy

• Java ORM libraries

• problems with porting ORM to Android

• slow

• complex

• missing functionalities

Intro(2)

• DBFlow

• open source library, developed by Andrew Grosner (Raizlabs)

• current stable version is 1.7.0

• available at https://github.com/Raizlabs/DBFlow

Intro(3)

• DBFlow

• Built on maximum performance using annotation processing and lazy-loading

• based on Active Android, Schematic, Ollie, and Sprinkles

• Every feature has been unit tested

Basic functions

Including in your project

root Gradle file

Including in your project

With Griddle

Without Griddle

Including in your project

Init & destroy FlowManager in Application class

Database configuration

Multi database support you can define more than one DB

Model creation(1)

Model creation rules• All Models MUST HAVE A DEFAULT CONSTRUCTOR

• Subclassing works as one would expect: the library gathers all inherited fields annotated with @Column and count those as rows in the current class's database.

• Column names default to the field name as a convenience

• All fields must be public or package private as the ModelAdapter class needs access to them.

• All model class definitions must be top-level (in their own file) and public or package private

Foreign key

columnName name of column in this table

columnType type of column in this table (must be same)

foreignKeyName name of this column in referenced table

Foreign key - lazy installation

Foreign key

• whenever the current table is saved, the foreign key object is saved

• for maximum performance gain, set saveForeignKeyModel() property on false - you’ll have to manage it yourself

Basic SQL queries

• DBFlow provides 2 ways for SQL Queries

• using wrapper classes

• using Transactions -> PREFERED

Basic SQL queries

• main difference is that wrapper classes are executed on current thread, while transaction uses TransactionQueue which has major effect on performance

• both can be used in sync or async mode

• callbacks are always called on UI thread

Wrapper classes

• quite similar to ActiveAndroid

• also can use TransactionQueue - DBFlow allows you to use library however you prefer (probably not good)

SELECT

UPDATE

DELETE

Other• Join statements

• order by

• having

• group by

• limit

• offset

Transactions

• utilises DBTransactionQueue, which is based on VolleyRequestQueue by using a PriorityBlockingQueue

• This queue will order our database transactions by priority (highest to lowest)

• 4 priorities - UI, HIGH, NORMAL, LOW

Transactions

Transactions• each transaction is described with ProccessModelInfo object

• name

• priority

• models

• For massive save() operations, use DBBatchSaveQueue - this will run a batch DBTransaction once the queue is full (default is 50 transactions)

• support for custom transactions

Advanced features

Observable models

Listen for changeson Model

create one observer and add as many

listeners as you want

Migrations

Migrations(2)

• don’t forget to increment your DB version!

Triggers

Indexes

More• Content providers - leverages DBFlow power

• Model caching

• Kotlin support

• prepackaged databases

• Model views

• and much more!!!

Benchmark tests

Benchmark tests• open source project, available at https://github.com/

Raizlabs/AndroidDatabaseLibraryComparison

• comparison between ActiveAndroid, SugarORM, Sprinkles

• tests are done on 25 000 records

• more on http://www.raizlabs.com/dev/2015/02/go-dbflow-fastest-android-orm-database-library/

Load

Save

Conclusion

Conclusion• IMHO currently the best ORM library

• large number of functionalities, every new version of library adds a few more

• active community

• complex documentation (currently no working example)

• steep learning curve

zeljko.plesac@infinum.co

skype: zeljko.plesac

twitter: @ZeljkoPlesac

top related