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

How to choose the ORM on Android

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!

FUJI Goro

March 10, 2017
Tweet

More Decks by FUJI Goro

Other Decks in Technology

Transcript

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

    2, 10:40-11:30 room 3 by [email protected] (@__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. Table of Contents I. Introduction to the Speaker II. What

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

    device and its preferences • Much faster than Web API • Much more reliable than Web API 
  8. 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 
  9. What You Stores to Storages • Preferences (or settings) •

    Caches • Drafts • Buffers (or queue) 
  10. 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.) 
  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. Built-in SQLiteDatabase • RDBMS • Interface to SQLite3 (C library)

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

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

    Association Management • Publish / Subscribe (pub-sub) • Migration 
  15. Query Builder • a.k.a. SQL builder • todos.equal(“title”, title) •

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

    todos.equal(Todo_Meta.title, title) • todos.titleEq(title) 
  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. Pub-Sub • Notifications on data-set changes • Used to synchronize

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

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

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

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

    ORMs Are III.Comparison of ORMs IV. How Orma Is Designed 
  24. 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
  25. 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
  26. 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
  27. Migration • In short: TERRIBLE • Just execute versioned SQL

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

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

    use • Not type safe • No longer maintained
  30. greenDAO • One of the most famous ORM in Android

    • Considered as high performance • Modeling was considered terrible • Based on gradle-plugin-based code generator
  31. 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
  32. Pros. and Cons. • Performance is awesome • Modifying source

    code seems really strange • Not so type safety • Migration is terrible
  33. Requery • a brand-new ORM for Android • v1.0.0 is

    released at 2016/09 • Annotation-processor-based code generator
  34. Migration • Add new columns automatically • but need schemaVersion++

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

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

    many abstraction • Supporting non-Android plarfotms are useless for us
  37. SQLBrite & SQLDelight • Developed by Square • SQLBrite: pub-sub

    helper to SQLite • SQLDelgith: generates models from DSL
  38. Pros. and Cons. • SQLBright is interesting if you use

    raw SQLiteOpenHelper • SQLDelight’s DSL is strange
  39. Realm Mobile Platform • Synchronized data within multiple devices •

    See “Offline-First Application Development” in DroidKaigi 217 day 1 by zaki50
  40. 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
  41. Table of Contents I. Introduction to the Speaker II. What

    ORMs are III. Comparison of ORMs IV. How Orma Is Designed 
  42. Orma • v1.0.0 is released at 2016/01 • Used in

    cookpad, Abema TV, mercari atte, pairs, droidkaigi 2017 • annotation-processor based code generator
  43. Query Builder • Todo_Selector (generated class) • Todo_Selector#idEq(long) • only

    for indexed columns • Todo_Selector#orderByIdDesc() • only for indexed columns
  44. Migration • schema-diff migration • ALTER TABLE from diff •

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

    delete columns • change the column constraints • Can’t: rename columns / tables
  46. 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
  47. Pros. and Cons. • SchemaDiffMigration is awesome • Type-safe query

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

    builder is awesome • Association is incubating • pub-sub is incubating