Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Getting into REAL(M) business by dumping SQLite

Dhrumil Shah
December 28, 2015

Getting into REAL(M) business by dumping SQLite

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.

Dhrumil Shah

December 28, 2015
Tweet

More Decks by Dhrumil Shah

Other Decks in Programming

Transcript

  1. Hello! I am Dhrumil Shah I work at Bugle Technologies

    Pvt. Ltd. & Co-organizer at GDG Ahmedabad. You can find me at: @dhuma1981 +DhrumilShah
  2. BoilerPlate mess ▷ 1000’s of lines of boilerplate mess ▷

    Lot of string just to define a single table ▷ Tightly coupled schema ▷ RAW SQL Queries
  3. Nested Queries SELECT table1.col, table3.col FROM table1 JOIN table2 ON

    table1.primarykey = table2.foreignkey JOIN table3 ON table2.primarykey = table3.foreignkey
  4. Nested Queries String query = “select” + table1.col + ”,”

    + table3.col + “FROM” + table1 + “JOIN” + table2 + “ON” + table1.primarykey + “=” + table2.foreignkey + “JOIN” + table3 + “ON” + table2.primarykey + “=” + table3.foreignkey
  5. ▷ SugarORM ▷ ORMLite ▷ CubBoard ▷ ActiveRecord ▷ Ollie

    ▷ DBFLow ORM Options ▷ DBQuery ▷ GreenDao ▷ EasyLiteORM ▷ RushORM ▷ SpringkilesBlack ▷ . . . .
  6. ▷ Easy setup ▷ Abstract SQLite boilerplate codes ▷ No

    direct dealing with SQL ▷ Object Mapping ORM Good Part
  7. ▷ 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
  8. Object Store SELECT table1.col, table3.col FROM table1 JOIN table2 ON

    table1.primarykey = table2. foreignkey JOIN table3 ON table2.primarykey = table3. foreignkey
  9. // 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
  10. 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<Person> queryResults = realm.allObjects(Person.class) queryResult.get(0) != queryResult.get(0) //different objects queryResult.get(0).equals(queryResult.get(0)) //same data Zero Copy
  11. ▷ Easy Setup ▷ Memory mapped files ▷ C++ core

    ▷ B+ trees ▷ Column Oriented ▷ Minimize IO overhead ▷ Cross platform ▷ Faster Read & writes ▷ Read > Writes ▷ Simple Querying interface Designed for Mobile
  12. Method and Size ▷ ~ 1750 method counts ▷ adds

    ~730 KB fat to apk ▷ install size less
  13. Realm Objects //Standard Java POJO from RealmObject public class Person

    extends RealmObject { @PrimaryKey private String personName; private int personAge; ….. …... //Getters and Setters }
  14. 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 }
  15. Writing Data Async Block //transaction block realm.executeTransaction(new Realm.Transaction() { @Override

    public void execute(Realm realm) { Person person = realm.createObject(Person.class); person.setName("John"); person.setEmail("[email protected]"); } });
  16. ▷ 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
  17. Query Data // Build the query looking at all users:

    RealmQuery<User> query = realm.where(User.class); // Add query conditions: query.equalTo("name", "John"); query.or().equalTo("name", "Peter"); // Execute the query: RealmResults<User> result1 = query.findAll(); // Or alternatively do the same all at once (the "Fluent interface"): RealmResults<User> result2 = realm.where(User.class) .equalTo("name", "John") .or() .equalTo("name", "Peter") .findAll();
  18. Query Data // Queries use Builder pattern to build up

    the query conditions RealmResults<Dog> query = realm.where(Dog.class) .greaterThan("age", 8) .findAll(); // Queries are chainable RealmResults<Dog> allRex = query.where() .contains("name", "rex") .findAll();
  19. ▷ Conditions like lessThan, greaterThan, contains, beginsWith etc ▷ Modifiers

    for ignoring Cases ▷ Logical Operations OR AND ▷ Sorting Ascending, Descending ▷ Chaining Queries ▷ Aggregations like SUM, MIN, MAX, AVG ▷ Synchronous, Asynchronous operations Query Data Supports
  20. 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 }
  21. Link Queries // You can now link queries together RealmResults<Contact>

    contacts = realm.where(Contact. class).equalTo("emails.active", true).findAll();
  22. ▷ 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
  23. ▷ 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
  24. Mac App to browse data stored on realm files (there

    is a separate plugin for STETHO)
  25. 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