infinum android talks #02 - activeandroid

21
ActiveAndroid Željko Plesac

Upload: infinum-ltd

Post on 06-Jul-2015

994 views

Category:

Technology


4 download

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

Page 1: Infinum Android Talks #02 - ActiveAndroid

ActiveAndroid

Željko Plesac

Page 2: Infinum Android Talks #02 - ActiveAndroid

Content

• Introduction!

• Setup!

• Usage!

• Pros & cons!

• Conclusion

Page 3: Infinum Android Talks #02 - ActiveAndroid

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

Page 4: Infinum Android Talks #02 - ActiveAndroid

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

Page 5: Infinum Android Talks #02 - ActiveAndroid

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

Page 6: Infinum Android Talks #02 - ActiveAndroid

2.3. Configuration(1)

• 2 - step configuration process:!

• add AA_DB_NAME and AA_DB_VERSION to AndroidManifest.xml!

Page 7: Infinum Android Talks #02 - ActiveAndroid

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

Page 8: Infinum Android Talks #02 - ActiveAndroid

3. Usage - data models (1)

• Why is primary key not defined?

Page 9: Infinum Android Talks #02 - ActiveAndroid

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”

Page 10: Infinum Android Talks #02 - ActiveAndroid

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

• Relationships - one to many

Page 11: Infinum Android Talks #02 - ActiveAndroid

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.

Page 12: Infinum Android Talks #02 - ActiveAndroid

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

Page 13: Infinum Android Talks #02 - ActiveAndroid

3. Usage - CRUD operations(1)• Create

• Delete

Page 14: Infinum Android Talks #02 - ActiveAndroid

3. Usage - CRUD operations(2)

• Select

• Update - make changes and call save() method

Page 15: Infinum Android Talks #02 - ActiveAndroid

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

Page 16: Infinum Android Talks #02 - ActiveAndroid

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

• innerJoin()!

• join()!

• leftJoin()!

• outerJoin()!

• limit()!

• groupBy()!

• orderBy()!

• having()!

• toSql()!

• offset()

Page 17: Infinum Android Talks #02 - ActiveAndroid

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

Page 18: Infinum Android Talks #02 - ActiveAndroid

4. Pros & Cons (2)

Page 19: Infinum Android Talks #02 - ActiveAndroid

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

Page 20: Infinum Android Talks #02 - ActiveAndroid

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

Page 21: Infinum Android Talks #02 - ActiveAndroid

www.activeandroid.com

https://github.com/pardom/ActiveAndroid