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

    View Slide

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

    View Slide

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

    View Slide

  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

    View Slide


  5. View Slide

  6. Released Kibela at 2017/3/1

    View Slide

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

    View Slide

  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

    View Slide

  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

    View Slide

  10. What You Stores to Storages
    • Preferences (or settings)
    • Caches
    • Drafts
    • Buffers (or queue)

    View Slide

  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.)

    View Slide

  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.)

    View Slide

  13. Built-in SQLiteDatabase
    • RDBMS
    • Interface to SQLite3 (C library)
    • Opetations with SQL (SQL-92)
    • Transactions

    View Slide

  14. ORMs
    • Object-Relation Mapper
    • Lots of implementations:
    • ActiveAndroid, DBFlow, Orma,
    Requery, ORMLite, greenDAO,
    SugarORM, StorIO, Realm, and etc.

    View Slide

  15. What ORMs do
    • O/R Mapping
    • Query Builder
    • Association Management
    • Publish / Subscribe (pub-sub)
    • Migration

    View Slide

  16. O/R Mapping
    deserialize↓↑serialize

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

  21. Association (or relathinship)
    • Handles table-to-table association
    • has-one / has-many / many-to-many

    View Slide

  22. Pub-Sub
    • Notifications on data-set changes
    • Used to synchronize model state
    • e.g. the state of “” over fragments

    View Slide

  23. Migration
    • How schema changes apply to the
    database
    • Calling ALTER TABLE, or completely re-
    create tables if needed
    • SQLiteOpenHelper’s / Java code /
    Automatic

    View Slide

  24. What are ORMs?
    ✅ O/R Mapping
    ✅ Query Builder
    ✅ Associations
    ✅ Publish / Subscribe (pub-sub)
    ✅ Migration

    View Slide

  25. As Open Source Software
    • README / document
    • CI / CD
    • Number of comitters
    • Changelogs / relase notes
    • Recent activities

    View Slide

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

    View Slide

  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

    View Slide

  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

    View Slide

  29. ActiveAndroid v3.0

    View Slide

  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

    View Slide

  31. Model Definition

    View Slide

  32. Usage

    View Slide

  33. Usage

    View Slide

  34. Usage

    View Slide

  35. Query Builder

    View Slide

  36. Association

    View Slide

  37. Association

    View Slide

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

    View Slide

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

    View Slide

  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

    View Slide

  41. As Open Source Software
    • Last commit: 2014/10
    • The last stable version tag: 2012/01
    • No longer maintained

    View Slide

  42. Pros. and Cons.

    Easy to setup / model / use

    Not type safe

    No longer maintained

    View Slide

  43. greenDAO v3.2.0

    View Slide

  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

    View Slide

  45. Model Definition

    View Slide

  46. Model Definition (after sync)

    View Slide

  47. Usage

    View Slide

  48. Usage

    View Slide

  49. Usage

    View Slide

  50. Query Builder

    View Slide

  51. Query Builder

    View Slide

  52. Query Builder

    View Slide

  53. Association

    View Slide

  54. Pub-Sub
    • Not supported

    View Slide

  55. Migration
    • Not supported

    View Slide

  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

    View Slide

  57. Pros. and Cons.

    Performance is awesome

    Modifying source code seems
    really strange
    • Not so type safety

    Migration is terrible

    View Slide

  58. Requery v1.2.0

    View Slide

  59. Requery
    • a brand-new ORM for Android
    • v1.0.0 is released at 2016/09
    • Annotation-processor-based code
    generator

    View Slide

  60. Model Definition

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

  64. Query Builder
    • where(Condition)
    • Todo.ID.eq(Integer)

    View Slide

  65. Association

    View Slide

  66. Pub-Sub
    • Supported with RxJava / RxJava2

    View Slide

  67. Migration
    • Add new columns automatically
    • but need schemaVersion++
    • Do nothing if constraints or the type
    change

    View Slide

  68. As Open Source Software
    • Very active
    • Not so kind for Android developers
    • because it supports lots of
    databases

    View Slide

  69. Pros. and Cons.

    RxJava support seems cool

    Too many abstraction

    Supporting non-Android
    plarfotms are useless for us

    View Slide

  70. SQLBright v0.6.2
    &
    SQLDelight v0.6.0

    View Slide

  71. SQLBrite & SQLDelight
    • Developed by Square
    • SQLBrite: pub-sub helper to SQLite
    • SQLDelgith: generates models from
    DSL

    View Slide

  72. SQLDelgiht DSL
    That’s SQL

    View Slide

  73. SQLDelgiht DSL
    That’s SQL
    !?

    View Slide

  74. Pros. and Cons.

    SQLBright is interesting if you
    use raw SQLiteOpenHelper

    SQLDelight’s DSL is strange

    View Slide

  75. Realm v3.0.0

    View Slide

  76. Realm (realm-java)
    • A superstar of ORMs
    • Not based on SQLite
    • Developed by Realm

    View Slide

  77. Model Definition

    View Slide

  78. Usage

    View Slide

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

    View Slide

  80. Association
    • Supported

    View Slide

  81. Pub-Sub
    • Supported

    View Slide

  82. Migration

    View Slide

  83. Migration

    View Slide

  84. Realm Mobile Platform
    • Synchronized data within multiple
    devices
    • See “Offline-First Application
    Development” in DroidKaigi 217
    day 1 by zaki50

    View Slide

  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

    View Slide

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

    View Slide

  87. Orma v4.2.1

    View Slide

  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

    View Slide

  89. Model Definition
    • No base class
    • Explicit annotations required

    View Slide

  90. Usage
    • Writing on ui-thread raises exceptions

    View Slide

  91. Usage
    • Writing on ui-thread raises exceptions

    View Slide

  92. Usage
    • Todo_Relation Todo_Relation#idEq(long)

    View Slide

  93. Usage
    • Todo_Relation Todo_Relation#idEq(long)

    View Slide

  94. Todo_Relation

    View Slide

  95. Todo_Relation
    [email protected]

    DPOUFOU&R lCBSz

    View Slide

  96. Todo_Relation
    [email protected]

    DPOUFOU&R lGPPz

    [email protected]

    DPOUFOU&R lCBSz

    View Slide

  97. Usage

    View Slide

  98. Query Builder
    • Todo_Selector (generated class)
    • Todo_Selector#idEq(long)
    • only for indexed columns
    • Todo_Selector#orderByIdDesc()
    • only for indexed columns

    View Slide

  99. Generated Code
    ↓ jump to definition

    View Slide

  100. Association

    View Slide

  101. Pu-Sub
    • Supported experimentally

    View Slide

  102. Migration
    • schema-diff migration
    • ALTER TABLE from diff
    • step-by-step migration
    • hand-written steps

    View Slide

  103. SchemaDiffMigration
    • Can detect
    • add columns / tables
    • delete columns
    • change the column constraints
    • Can’t: rename columns / tables

    View Slide

  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

    View Slide

  105. Versioning
    • Migration is triggered on
    SCHEMA_HASH is changed
    • SCHEMA_HASH = SHA256(DDL)

    View Slide

  106. Pros. and Cons.

    SchemaDiffMigration is awesome

    View Slide

  107. Pros. and Cons.

    SchemaDiffMigration is awesome

    Type-safe query builder is awesome

    View Slide

  108. Pros. and Cons.

    SchemaDiffMigration is awesome

    Type-safe query builder is awesome

    Association is incubating

    View Slide

  109. Pros. and Cons.

    SchemaDiffMigration is awesome

    Type-safe query builder is awesome

    Association is incubating

    pub-sub is incubating

    View Slide

  110. Any Questions?

    View Slide