40
We design and develop great software for mobile and web.

Infinum Android Talks #09 - DBFlow ORM

Embed Size (px)

Citation preview

Page 1: Infinum Android Talks #09 - DBFlow ORM

We design and develop great software for mobile and web.

Page 2: Infinum Android Talks #09 - DBFlow ORM

DBFlow ORM library

Željko Plesac Android talks #10

Page 3: Infinum Android Talks #09 - DBFlow ORM

Scope

• Intro

• Basic functions

• Advanced functions

• Benchmark tests

• Conclusion

Page 4: Infinum Android Talks #09 - DBFlow ORM

Intro• debugging SQL errors is not easy

• Java ORM libraries

• problems with porting ORM to Android

• slow

• complex

• missing functionalities

Page 5: Infinum Android Talks #09 - DBFlow ORM

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

Page 6: Infinum Android Talks #09 - DBFlow ORM

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

Page 7: Infinum Android Talks #09 - DBFlow ORM

Basic functions

Page 8: Infinum Android Talks #09 - DBFlow ORM

Including in your project

root Gradle file

Page 9: Infinum Android Talks #09 - DBFlow ORM

Including in your project

With Griddle

Without Griddle

Page 10: Infinum Android Talks #09 - DBFlow ORM

Including in your project

Init & destroy FlowManager in Application class

Page 11: Infinum Android Talks #09 - DBFlow ORM

Database configuration

Multi database support you can define more than one DB

Page 12: Infinum Android Talks #09 - DBFlow ORM

Model creation(1)

Page 13: Infinum Android Talks #09 - DBFlow ORM

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

Page 14: Infinum Android Talks #09 - DBFlow ORM

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

Page 15: Infinum Android Talks #09 - DBFlow ORM

Foreign key - lazy installation

Page 16: Infinum Android Talks #09 - DBFlow ORM

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

Page 17: Infinum Android Talks #09 - DBFlow ORM

Basic SQL queries

• DBFlow provides 2 ways for SQL Queries

• using wrapper classes

• using Transactions -> PREFERED

Page 18: Infinum Android Talks #09 - DBFlow ORM

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

Page 19: Infinum Android Talks #09 - DBFlow ORM

Wrapper classes

• quite similar to ActiveAndroid

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

Page 20: Infinum Android Talks #09 - DBFlow ORM

SELECT

Page 21: Infinum Android Talks #09 - DBFlow ORM

UPDATE

Page 22: Infinum Android Talks #09 - DBFlow ORM

DELETE

Page 23: Infinum Android Talks #09 - DBFlow ORM

Other• Join statements

• order by

• having

• group by

• limit

• offset

Page 24: Infinum Android Talks #09 - DBFlow ORM

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

Page 25: Infinum Android Talks #09 - DBFlow ORM

Transactions

Page 26: Infinum Android Talks #09 - DBFlow ORM

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

Page 27: Infinum Android Talks #09 - DBFlow ORM

Advanced features

Page 28: Infinum Android Talks #09 - DBFlow ORM

Observable models

Listen for changeson Model

create one observer and add as many

listeners as you want

Page 29: Infinum Android Talks #09 - DBFlow ORM

Migrations

Page 30: Infinum Android Talks #09 - DBFlow ORM

Migrations(2)

• don’t forget to increment your DB version!

Page 31: Infinum Android Talks #09 - DBFlow ORM

Triggers

Page 32: Infinum Android Talks #09 - DBFlow ORM

Indexes

Page 33: Infinum Android Talks #09 - DBFlow ORM

More• Content providers - leverages DBFlow power

• Model caching

• Kotlin support

• prepackaged databases

• Model views

• and much more!!!

Page 34: Infinum Android Talks #09 - DBFlow ORM

Benchmark tests

Page 35: Infinum Android Talks #09 - DBFlow ORM

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/

Page 36: Infinum Android Talks #09 - DBFlow ORM

Load

Page 37: Infinum Android Talks #09 - DBFlow ORM

Save

Page 38: Infinum Android Talks #09 - DBFlow ORM

Conclusion

Page 39: Infinum Android Talks #09 - DBFlow ORM

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

Page 40: Infinum Android Talks #09 - DBFlow ORM

[email protected]

skype: zeljko.plesac

twitter: @ZeljkoPlesac