Selecting Best persistent storage for Phimpme Android and how to use it
As we are progressing in our Phimpme Android app. I added account manager part which deals with connecting all other accounts to phimpme. Showing a list of connected accounts. We need a persistent storage to store all the details such as username, full name, profile image url, access token (to access API). I researched on various Object Relation mapping (ORMs) such as: DBFlow: https://github.com/Raizlabs/DBFlow GreenDAO: https://github.com/greenrobot/greenDAO SugarORM: http://satyan.github.io/sugar/ Requery: https://github.com/requery/requery and other NoSQL databases such as Realm Database : https://github.com/realm/realm-java. After reading a lot from some blogs on the benchmarking of these ORMs and database, I came to know that Realm database is quite better in terms of Speed of writing data and ease of use. Steps to integrate Realm Database: Installation of Realm database in android Following these steps https://realm.io/docs/java/latest/#installation quickly setup realm in android. Add classpath "io.realm:realm-gradle-plugin:3.3.2" in Project level build.gradle file and Add apply plugin: 'realm-android' in app level build.gradle, That’s it for using Realm Generating required Realm models Firstly, make sure what you need to store in your database. In case of phimpme, I first go through the account section and noted down what needs to be there. Profile image URL, username, full name, account indicator image name. Below image illustrate this better. This is the Realm Model class I made in Kotlin to store name, username and access token for accessing API. open class AccountDatabase( @PrimaryKey var name: String = "", var username: String = "", var token: String = "" ) : RealmObject() Writing data in database In Account manager, I create a add account option from where a dialog appear with a list of accounts. Currently, Twitter is working, when onSuccess function invoke in AccountPickerFragment I start a twitter session and store values in database. Writing data in database: // Begin realm transaction realm.beginTransaction(); // Creating Realm object for AccountDatabase Class account = realm.createObject(AccountDatabase.class, accountsList[0]); account.setUsername(session.getUserName()); account.setToken(String.valueOf(session.getAuthToken())); realm.commitTransaction(); Begin and commit block in necessary. There is one more way of doing this is using execute function in Realm Use Separate Database Helper class for Database operations It’s good to use a separate class for all the Database operations needed in the project. I created a DatabaseHelper Class and added a function to query the result needed. Query the database public RealmResults<AccountDatabase> fetchAccountDetails(){ return realm.where(AccountDatabase.class).findAll(); } It give all of the results, stored in the database like below Problems I faced with annotation processor while using Kotlin and Realm together The Kotlin annotation processor not running due to the plugins wrong order. This issue https://github.com/realm/realm-java/pull/2568 helped me in solving that. I addded apply plugin: 'kotlin-kapt'. In app gradle file and shift apply plugin: 'realm-android' In below the order. Resources: Realm for Android (official): https://news.realm.io/news/realm-for-android/ Write in Realm Database: https://realm.io/docs/java/latest/#writes Queries in Realm Database: https://realm.io/docs/java/latest/#queries A detailed Blog post on Realm Database: https://medium.com/@Zhuinden/how-to-use-realm-for-android-like-a-champ-and-how-to-tell-if-youre-doing-it-wrong-ac4f66b7f149
