Infinum Android Talks #02 - ActiveAndroid

Post on 06-Jul-2015

994 views 4 download

Tags:

description

ActiveAndroid is lightweight ORM library for Android, which we use in few of our projects. I'll demonstrate how to setup the library, and conclude with few examples of its usage.

Transcript of Infinum Android Talks #02 - ActiveAndroid

ActiveAndroid

Željko Plesac

Content

• Introduction!

• Setup!

• Usage!

• Pros & cons!

• Conclusion

1.Introduction

• ActiveAndroid is an active record style ORM !

• allows you to save and retrieve SQLite database records without ever writing a single SQL statement. Each database record is wrapped neatly into a class with methods like save() and delete().!

• developed by Michael Pardo

2. Setup - JAR file(1)

• Obtain JAR file by cloning the source or downloading it, you can build the jar file by running ant in the root folder. Your ActiveAndroid.jar, which is what we need, will be in the dist folder. Add this .jar file to your project’s build path

2. Setup - Maven (2)

• clone the source from Git and install the package to your local repository!

• After the project builds successfully, add the following dependency to your pom.xml

2.3. Configuration(1)

• 2 - step configuration process:!

• add AA_DB_NAME and AA_DB_VERSION to AndroidManifest.xml!

2.4 Configuration(2)• If you are using a custom Application class, extend

com.activeandroid.app.Application instead of android.app.Application!

• If you are already using this to utilize another library, initialise and dispose ActiveAndroid in Application class

3. Usage - data models (1)

• Why is primary key not defined?

3. Usage - data models (2)

• ActiveAndroid assigns automatically primary key to every database object!

• All primary keys are of type Long, and their name is “Id”

3. Usage - data models(3)• Relationships - one to one

• Relationships - one to many

Usage - data models(4)• Database tables are created when you firstly save your

database model(based on current class structure) !

• Once the database schema has been created, it can’t be automatically changed(but new models can be added)!

• Whenever your database schema changes, you must:!

• increment the database version number, either through Configuration or AA_DB_VERSION meta-data.!

• provide a script <NewVersion>.sql in /assets/migrations.

Usage - data models(5)• Example of <NewVersion>.sql script

3. Usage - CRUD operations(1)• Create

• Delete

3. Usage - CRUD operations(2)

• Select

• Update - make changes and call save() method

Usage - CRUD operations(3)• Transactions - speed up execution by a factor of around 100

3. Usage - other supported actions• crossJoin()!

• innerJoin()!

• join()!

• leftJoin()!

• outerJoin()!

• limit()!

• groupBy()!

• orderBy()!

• having()!

• toSql()!

• offset()

4. Pros & Cons (1)• Pros!

• quick and easy setup!

• easy maintenance!

• separation between data models and DAO objects!

• can be used in combination with GSON!

• custom type casters!

• pre populated databases

4. Pros & Cons (2)

4. Pros & Cons (3)• Cons!

• not suitable for big data - even wrapped in transactions, CRUD actions can take a lot of time!

• it doesn’t provide real administration privileges!

• problems with Proguard!

• it doesn’t support lambda expressions!

• it only supports code-first approach

5. Conclusion

• Highly recommended in combination with GSON!

• It doesn’t provide so much tools and options like competition (ORMLite and Springles)!

• Easy and simple to use

www.activeandroid.com

https://github.com/pardom/ActiveAndroid