Presented this talk at DevFest Ahmedabad 2015 (#DevFestAhm). In this talk I have covered what is Realm? Why you should use Realm over SQLite and ORM with code snippets.
▷ Slow, Most ORM uses reflections, read write performance is slow ▷ Not entirely SQL free, still need to write queries for certain task ▷ And as underlying is SQLite all the issues with database scaling. ORM Bad Part
// create Cursor in order to parse our sqlite results Cursor cursor = sqliteDB.rawQuery("SELECT bookTitle FROM " + tableName, null); // if Cursor contains results if (cursor != null) { // move cursor to first row if (cursor.moveToFirst()) { do { // Get version from Cursor String bookName = cursor.getString(cursor.getColumnIndex("bookTitle")); } while (cursor.moveToNext()); } } What is Zero Copy
Person person = new Person() //Standard Java Person proxyperson = realm.copyToRealm(person) //Proxy object Person proxyperson = realm.createObject(Person.class) //Proxy object //Query Results are Lazy RealmResults queryResults = realm.allObjects(Person.class) queryResult.get(0) != queryResult.get(0) //different objects queryResult.get(0).equals(queryResult.get(0)) //same data Zero Copy
Realm Objects //Standard Java POJO from RealmObject public class Person extends RealmObject { @PrimaryKey private String personName; private int personAge; ….. …... //Getters and Setters }
Writing Data //Standard Java extend person from RealmObject Person person = new Person() //call setter to set data …. //save data try{ realm.beginTransaction(); realm.copyToRealm(person); realm.commitTransaction(); }catch(Exception e){ realm.cancelTransaction(); // rollback data }
▷ Supports Primary Key ▷ Supports primitive data types like int, long, boolean etc. ▷ Supports Box types like Integer, Long, String etc. ▷ Allows null value for non primitives types ▷ Supports Indexing Realm Object
Query Data // Build the query looking at all users: RealmQuery query = realm.where(User.class); // Add query conditions: query.equalTo("name", "John"); query.or().equalTo("name", "Peter"); // Execute the query: RealmResults result1 = query.findAll(); // Or alternatively do the same all at once (the "Fluent interface"): RealmResults result2 = realm.where(User.class) .equalTo("name", "John") .or() .equalTo("name", "Peter") .findAll();
Relationships // Any Realm Object can be linked together public class Email extends RealmObject { private String address; private boolean active; // ... setters and getters left out } public class Contact extends RealmObject { private String name; private Email email; // ... setters and getters left out }
▷ Direct support for Json to Object store ▷ Notification for database change like CP ▷ Encryption support in built ▷ Adapters for binding data with views ▷ Cross Platform, same data can work across iOS and Android ▷ Robust 3rd parties addons Other Features
▷ Not Entirely NOSQL, need migration on schema Changes ▷ No support for auto incrementing primary keys ▷ Closed source code ▷ Mutable, thread-confined objects ▷ No support for realm objects across threads ▷ Slower write ▷ Increase method count and size of apk Limitations
Credits Special thanks to all the people who made and released these awesome resources for free: ▷ Realm by realm.io ▷ Christian Melchior from Realm Team ▷ Pranay Airan from GDG BlrDroid ▷ Presentation template by SlidesCarnival