Let’s start the exploring the GreenDao ORM database to integrate with RxJava in Android application. GreenDAO is a fast ORM (Object-Relational Mapping) tool for Android. GreenDAO offers an object-oriented interface with relation database SQLite. It is fast to other ORM that why it is something special and different. GreenDAO is work on two basic things. One is core Dao and another is Dao Generator. Only the first one is packed into your app and the second is only used for class generation (you won’t need or use it directly in your app). This is definitely something that makes GreenDAO so special.

For those who haven’t heard, this is a great open-source Android ORM solution which enables you to forget about SQLite low-level stuff and concentrate on the database’s overall life cycle, models and how do they relate. You don’t actually need to bother how to populate the database and how the relations work because greenDAO handles it for you. All you need to know is what you want to keep in your DB.

DaoMaster: DaoMaster holds the database object (SQLiteDatabase) and manages DAO classes (not objects) for a specific schema. It has static methods to create the tables or drop them. Its inner classes OpenHelper and DevOpenHelper are SQLiteOpenHelper implementations that create the schema in the SQLite database.

DaoSession: Manages all available DAO objects for a specific schema, which you can acquire using one of the getter methods. DaoSession provides also some generic persistence methods like insert, load, update, refresh and delete for entities. Lastly, a DaoSession object also keeps track of an identity scope.

DAOs: Data access objects (DAOs) persists and queries for entities. For each entity, GreenDAO generates a DAO. It has more persistence methods than DaoSession, for example, count, loadAll, and insertInTx.

Ok, Let’s integrate the Green Dao database ORM in one of the demo application. Very first thing we need to add the relevant plugin and dependency in the gradle file. Basically, I am using with RxJava then we need to add the RxJava dependency also.

1

2

3

4

5

compile'io.reactivex.rxjava2:rxandroid:2.0.1'

compile'io.reactivex.rxjava2:rxjava:2.0.8'

compile'org.greenrobot:greendao:3.2.0'

compile'net.zetetic:android-database-sqlcipher:3.5.1'

1

apply plugin:'org.greenrobot.greendao'

Now we need to add a Java Library Module for GreenDAO. So here I am creating new Module Java library name “greendaodb” and add the plugin in the gradle file.

1

compile'org.greenrobot:greendao-generator:3.2.0'

Let’s create the Entity for Dao main, which will create the database name and table with automated generated Dao files and Dao session. And make sure that you have given target directory in your gradle file to generate the Dao files there. GreenDAO also provides the table mapping very simple and easy way, for example, one-to-one , one-to-many or many-to-one. You get the detail from the official document for relational mapping.

You should update the version number just to keep track of the updates. Also, we enable the keep sections, for the ability to modify our models without the fear that those modifications will be overridden by a next generation. It means When we required adding our custom methods or models inside the DAO we can add. But once we update the database version it will recreate all generated files again. In this case, our added custom method will be removed. So We should keep those methods or any thing we need to pointed out to DAO is to keep sections as it is while generating those Dao files again.

Security thing always on top of developing android application So GreenDAO provides the security feature which will be encrypted your database. Let’s implement the encrypted database.

Ok, GreenDaoDatabse class is ready, Now you need to create the instance of this class in MainApplication class.

1

GreenDaoDatabase.getInstance().init(this);

Let’s define the interface of all my operation wants to execute in our application, for example, insert data into a table and update the row of data or delete the row of the table. Here are the defined methods which I want to execute.

That’s it! We have successfully integrated GreenDAO into our Android application! If you would like to add other tables, go to the generator class, edit the entities, and run the generator class. This will generate the DAO files and other entities needed.

Wrapping Up: GreenDAO is one of the many Android ORM database libraries available. What makes it stand out is its performance and advanced caching support. It’s being actively developed and new features are added with each new release. Here are the best offer and recommendation for you to download this. You can get the full source code from Github.

I am a very enthusiastic Android developer to build solid Android apps. I have a keen interest in developing for Android and have published apps to the Google Play Store. I always open to learning new technologies. For any help drop us a line anytime at contact@mobologicplus.com

Categories

For better assist

Hey Folks, Welcome to the MobologicPlus blog. I’m Sunil Gupta, a tech blogger from Bangalore, India.
I started MobologicPlus as a passion, by helping people to understand the technical kinds of stuff from this blog. You have some feedback for MobologicPlus. Where should I send it? Sure, drop us a line anytime at contact@mobologicplus.com