How to choose the ORM on Android

9278c3a06b8d8752fb913dea93f959c1?s=47 FUJI Goro
March 10, 2017

How to choose the ORM on Android

I am the author of Orma. This presentation shows how Android ORMs are designed and how to choose your favorite. Enjoy ORMs!

9278c3a06b8d8752fb913dea93f959c1?s=128

FUJI Goro

March 10, 2017
Tweet

Transcript

  1. How to Choose the ORM on Android DroidKaigi 2017, day

    2, 10:40-11:30 room 3 by goro-fuji@bitjourney.com (@__gfx__)  #DroidKaigi3
  2. Table of Contents I. Introduction to the Speaker II. What

    ORMs Are III. Comparison of ORMs IV. How Orma Is Designed 
  3. Table of Contents I. Introduction to the Speaker II. What

    ORMs Are III. Comparison of ORMs IV. How Orma Is Designed 
  4. Introduction to the Speaker • Rails Engineer at Bit Jouryney,

    Inc. • Released Kibela (kibe.la) recently • Mobile Application Engineer at Speee, Inc. • As a Technical Advisor • Loves Programming, Cooking and Gaming 
  5. 

  6. Released Kibela at 2017/3/1 

  7. Table of Contents I. Introduction to the Speaker II. What

    ORMs Are III. Comparison of ORMs IV. How Orma Is Designed 
  8. Why You Stores Data to Local • To identify the

    device and its preferences • Much faster than Web API • Much more reliable than Web API 
  9. Why You Stores Data to Local • To identify the

    device and its preferences • Much faster than Web API -> cache • Much more reliable than Web API -> offline-first development 
  10. What You Stores to Storages • Preferences (or settings) •

    Caches • Drafts • Buffers (or queue) 
  11. Data Storage on Android • Built-in SharedPreferences • Built-in File

    with the filesystem • Built-in SQLiteDatabase (SQLite) • SQLite wrappers (ORMs) • Original DB (Realm, ObjectBox, etc.) 
  12. Data Storage on Android • Built-in SharedPreferences • Built-in File

    with the filesystem • Built-in SQLiteDatabase (SQLite) • SQLite wrappers (ORMs) • Original DB (Realm, ObjectBox, etc.) 
  13. Built-in SQLiteDatabase • RDBMS • Interface to SQLite3 (C library)

    • Opetations with SQL (SQL-92) • Transactions 
  14. ORMs • Object-Relation Mapper • Lots of implementations: • ActiveAndroid,

    DBFlow, Orma, Requery, ORMLite, greenDAO, SugarORM, StorIO, Realm, and etc. 
  15. What ORMs do • O/R Mapping • Query Builder •

    Association Management • Publish / Subscribe (pub-sub) • Migration 
  16. O/R Mapping deserialize↓↑serialize 

  17. Query Builder • a.k.a. SQL builder • todos.equal(“title”, title) •

    todos.equal(Todo_Meta.title, title) • todos.titleEq(title) 
  18. Query Builder • a.k.a. SQL builder • todos.equal(“title”, title) •

    todos.equal(Todo_Meta.title, title) • todos.titleEq(title) 
  19. Query Builder • a.k.a. SQL builder • todos.equal(“title”, title) •

    todos.equal(Todo_Meta.title, title) • todos.titleEq(title) 
  20. Query Builder • a.k.a. SQL builder • todos.equal(“title”, title) •

    todos.equal(Todo_Meta.title, title) • todos.titleEq(title) 
  21. Association (or relathinship) • Handles table-to-table association • has-one /

    has-many / many-to-many 
  22. Pub-Sub • Notifications on data-set changes • Used to synchronize

    model state • e.g. the state of “” over fragments 
  23. Migration • How schema changes apply to the database •

    Calling ALTER TABLE, or completely re- create tables if needed • SQLiteOpenHelper’s / Java code / Automatic
  24. What are ORMs? ✅ O/R Mapping ✅ Query Builder ✅

    Associations ✅ Publish / Subscribe (pub-sub) ✅ Migration 
  25. As Open Source Software • README / document • CI

    / CD • Number of comitters • Changelogs / relase notes • Recent activities
  26. Table of Contents I. Introduction to the Speaker II. What

    ORMs Are III.Comparison of ORMs IV. How Orma Is Designed 
  27. Comparison of ORMs • ActiveAndroid v3.0 • greenDAO v3.2.0 •

    Requery v1.2.0 • SQLBrite v0.6.2 & SQLDelight v0.6.0 • Realm v3.0.0 • Orma v4.2.1
  28. What to See A. How O/R Mapping Is B. The

    Interface of Query Builder C. Association Support D. Pub-Sub Support E. Migration F. Performance G. As Open Source Software
  29. ActiveAndroid v3.0

  30. ActiveAndroid • One of the most famous ORM in Android

    • Considered as easy to use • No longer maintained • Baseline for many of ORMs • Based on reflection
  31. Model Definition

  32. Usage

  33. Usage

  34. Usage

  35. Query Builder

  36. Association

  37. Association

  38. Has-Many Association getMany() is useless because it returns List<T>

  39. Pub-Sub: via ContentProvider The end of Model#save() method: ↓

  40. Migration • In short: TERRIBLE • Just execute versioned SQL

    files • In development: you have to uninstall the app for each time you change the schema
  41. As Open Source Software • Last commit: 2014/10 • The

    last stable version tag: 2012/01 • No longer maintained
  42. Pros. and Cons. • Easy to setup / model /

    use • Not type safe • No longer maintained
  43. greenDAO v3.2.0

  44. greenDAO • One of the most famous ORM in Android

    • Considered as high performance • Modeling was considered terrible • Based on gradle-plugin-based code generator
  45. Model Definition

  46. Model Definition (after sync)

  47. Usage

  48. Usage

  49. Usage

  50. Query Builder

  51. Query Builder

  52. Query Builder

  53. Association

  54. Pub-Sub • Not supported

  55. Migration • Not supported

  56. As Open Source Software • Last commit: 2017/03 • Last

    stable release: 2016/10 • @greenbot is developing another ORM: • ObjectBox, a brand-new mobile database with source-code compatibility with greenDAO
  57. Pros. and Cons. • Performance is awesome • Modifying source

    code seems really strange • Not so type safety • Migration is terrible
  58. Requery v1.2.0

  59. Requery • a brand-new ORM for Android • v1.0.0 is

    released at 2016/09 • Annotation-processor-based code generator
  60. Model Definition ↓

  61. Usage • DatabaseSouce extends SQLiteOpenHelper • Models is a generated

    class
  62. Usage • DatabaseSouce extends SQLiteOpenHelper • Models is a generated

    class
  63. Usage • DatabaseSouce extends SQLiteOpenHelper • Models is a generated

    class
  64. Query Builder • where(Condition<V, ?>) • Todo.ID.eq(Integer)

  65. Association

  66. Pub-Sub • Supported with RxJava / RxJava2

  67. Migration • Add new columns automatically • but need schemaVersion++

    • Do nothing if constraints or the type change
  68. As Open Source Software • Very active • Not so

    kind for Android developers • because it supports lots of databases
  69. Pros. and Cons. • RxJava support seems cool • Too

    many abstraction • Supporting non-Android plarfotms are useless for us
  70. SQLBright v0.6.2 & SQLDelight v0.6.0

  71. SQLBrite & SQLDelight • Developed by Square • SQLBrite: pub-sub

    helper to SQLite • SQLDelgith: generates models from DSL
  72. SQLDelgiht DSL That’s SQL

  73. SQLDelgiht DSL That’s SQL !?

  74. Pros. and Cons. • SQLBright is interesting if you use

    raw SQLiteOpenHelper • SQLDelight’s DSL is strange
  75. Realm v3.0.0

  76. Realm (realm-java) • A superstar of ORMs • Not based

    on SQLite • Developed by Realm
  77. Model Definition

  78. Usage

  79. Query Builder • equalTo() is not type-safe

  80. Association • Supported

  81. Pub-Sub • Supported

  82. Migration

  83. Migration

  84. Realm Mobile Platform • Synchronized data within multiple devices •

    See “Offline-First Application Development” in DroidKaigi 217 day 1 by zaki50
  85. Pros. and Cons. • Users do not use generated classes,

    and thus the interface is not type safe • Full-time comitters are awesome • Migration is terrible • Query builder is not type safe
  86. Table of Contents I. Introduction to the Speaker II. What

    ORMs are III. Comparison of ORMs IV. How Orma Is Designed 
  87. Orma v4.2.1

  88. Orma • v1.0.0 is released at 2016/01 • Used in

    cookpad, Abema TV, mercari atte, pairs, droidkaigi 2017 • annotation-processor based code generator
  89. Model Definition • No base class • Explicit annotations required

  90. Usage • Writing on ui-thread raises exceptions

  91. Usage • Writing on ui-thread raises exceptions

  92. Usage • Todo_Relation Todo_Relation#idEq(long)

  93. Usage • Todo_Relation Todo_Relation#idEq(long)

  94. Todo_Relation

  95. Todo_Relation 5PEP@3FMBUJPO  DPOUFOU&R lCBSz

  96. Todo_Relation 5PEP@3FMBUJPO  DPOUFOU&R lGPPz 5PEP@3FMBUJPO  DPOUFOU&R lCBSz

  97. Usage

  98. Query Builder • Todo_Selector (generated class) • Todo_Selector#idEq(long) • only

    for indexed columns • Todo_Selector#orderByIdDesc() • only for indexed columns
  99. Generated Code ↓ jump to definition

  100. Association

  101. Pu-Sub • Supported experimentally

  102. Migration • schema-diff migration • ALTER TABLE from diff •

    step-by-step migration • hand-written steps
  103. SchemaDiffMigration • Can detect • add columns / tables •

    delete columns • change the column constraints • Can’t: rename columns / tables
  104. sqlite_master $ sqlite3 foo.db sqlite> create table foo(id integer primary

    key); sqlite> select * from sqlite_master; table|foo|foo|2|CREATE TABLE foo(id integer primary key) • DDL are available via sqlite_master
  105. Versioning • Migration is triggered on SCHEMA_HASH is changed •

    SCHEMA_HASH = SHA256(DDL)
  106. Pros. and Cons. • SchemaDiffMigration is awesome

  107. Pros. and Cons. • SchemaDiffMigration is awesome • Type-safe query

    builder is awesome
  108. Pros. and Cons. • SchemaDiffMigration is awesome • Type-safe query

    builder is awesome • Association is incubating
  109. Pros. and Cons. • SchemaDiffMigration is awesome • Type-safe query

    builder is awesome • Association is incubating • pub-sub is incubating
  110. Any Questions?