Slide 1

Slide 1 text

How to Choose the ORM on Android DroidKaigi 2017, day 2, 10:40-11:30 room 3 by [email protected] (@__gfx__) #DroidKaigi3

Slide 2

Slide 2 text

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

Slide 3

Slide 3 text

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

Slide 4

Slide 4 text

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

Slide 5

Slide 5 text

Slide 6

Slide 6 text

Released Kibela at 2017/3/1

Slide 7

Slide 7 text

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

Slide 8

Slide 8 text

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

Slide 9

Slide 9 text

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

Slide 10

Slide 10 text

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

Slide 11

Slide 11 text

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

Slide 12

Slide 12 text

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

Slide 13

Slide 13 text

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

Slide 14

Slide 14 text

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

Slide 15

Slide 15 text

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

Slide 16

Slide 16 text

O/R Mapping deserialize↓↑serialize

Slide 17

Slide 17 text

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

Slide 18

Slide 18 text

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

Slide 19

Slide 19 text

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

Slide 20

Slide 20 text

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

Slide 21

Slide 21 text

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

Slide 22

Slide 22 text

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

Slide 23

Slide 23 text

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

Slide 24

Slide 24 text

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

Slide 25

Slide 25 text

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

Slide 26

Slide 26 text

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

Slide 27

Slide 27 text

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

Slide 28

Slide 28 text

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

Slide 29

Slide 29 text

ActiveAndroid v3.0

Slide 30

Slide 30 text

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

Slide 31

Slide 31 text

Model Definition

Slide 32

Slide 32 text

Usage

Slide 33

Slide 33 text

Usage

Slide 34

Slide 34 text

Usage

Slide 35

Slide 35 text

Query Builder

Slide 36

Slide 36 text

Association

Slide 37

Slide 37 text

Association

Slide 38

Slide 38 text

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

Slide 39

Slide 39 text

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

Slide 40

Slide 40 text

Migration • In short: TERRIBLE • Just execute versioned SQL files • In development: you have to uninstall the app for each time you change the schema

Slide 41

Slide 41 text

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

Slide 42

Slide 42 text

Pros. and Cons. • Easy to setup / model / use • Not type safe • No longer maintained

Slide 43

Slide 43 text

greenDAO v3.2.0

Slide 44

Slide 44 text

greenDAO • One of the most famous ORM in Android • Considered as high performance • Modeling was considered terrible • Based on gradle-plugin-based code generator

Slide 45

Slide 45 text

Model Definition

Slide 46

Slide 46 text

Model Definition (after sync)

Slide 47

Slide 47 text

Usage

Slide 48

Slide 48 text

Usage

Slide 49

Slide 49 text

Usage

Slide 50

Slide 50 text

Query Builder

Slide 51

Slide 51 text

Query Builder

Slide 52

Slide 52 text

Query Builder

Slide 53

Slide 53 text

Association

Slide 54

Slide 54 text

Pub-Sub • Not supported

Slide 55

Slide 55 text

Migration • Not supported

Slide 56

Slide 56 text

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

Slide 57

Slide 57 text

Pros. and Cons. • Performance is awesome • Modifying source code seems really strange • Not so type safety • Migration is terrible

Slide 58

Slide 58 text

Requery v1.2.0

Slide 59

Slide 59 text

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

Slide 60

Slide 60 text

Model Definition ↓

Slide 61

Slide 61 text

Usage • DatabaseSouce extends SQLiteOpenHelper • Models is a generated class

Slide 62

Slide 62 text

Usage • DatabaseSouce extends SQLiteOpenHelper • Models is a generated class

Slide 63

Slide 63 text

Usage • DatabaseSouce extends SQLiteOpenHelper • Models is a generated class

Slide 64

Slide 64 text

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

Slide 65

Slide 65 text

Association

Slide 66

Slide 66 text

Pub-Sub • Supported with RxJava / RxJava2

Slide 67

Slide 67 text

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

Slide 68

Slide 68 text

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

Slide 69

Slide 69 text

Pros. and Cons. • RxJava support seems cool • Too many abstraction • Supporting non-Android plarfotms are useless for us

Slide 70

Slide 70 text

SQLBright v0.6.2 & SQLDelight v0.6.0

Slide 71

Slide 71 text

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

Slide 72

Slide 72 text

SQLDelgiht DSL That’s SQL

Slide 73

Slide 73 text

SQLDelgiht DSL That’s SQL !?

Slide 74

Slide 74 text

Pros. and Cons. • SQLBright is interesting if you use raw SQLiteOpenHelper • SQLDelight’s DSL is strange

Slide 75

Slide 75 text

Realm v3.0.0

Slide 76

Slide 76 text

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

Slide 77

Slide 77 text

Model Definition

Slide 78

Slide 78 text

Usage

Slide 79

Slide 79 text

Query Builder • equalTo() is not type-safe

Slide 80

Slide 80 text

Association • Supported

Slide 81

Slide 81 text

Pub-Sub • Supported

Slide 82

Slide 82 text

Migration

Slide 83

Slide 83 text

Migration

Slide 84

Slide 84 text

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

Slide 85

Slide 85 text

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

Slide 86

Slide 86 text

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

Slide 87

Slide 87 text

Orma v4.2.1

Slide 88

Slide 88 text

Orma • v1.0.0 is released at 2016/01 • Used in cookpad, Abema TV, mercari atte, pairs, droidkaigi 2017 • annotation-processor based code generator

Slide 89

Slide 89 text

Model Definition • No base class • Explicit annotations required

Slide 90

Slide 90 text

Usage • Writing on ui-thread raises exceptions

Slide 91

Slide 91 text

Usage • Writing on ui-thread raises exceptions

Slide 92

Slide 92 text

Usage • Todo_Relation Todo_Relation#idEq(long)

Slide 93

Slide 93 text

Usage • Todo_Relation Todo_Relation#idEq(long)

Slide 94

Slide 94 text

Todo_Relation

Slide 95

Slide 95 text

Todo_Relation 5PEP@3FMBUJPO DPOUFOU&R lCBSz

Slide 96

Slide 96 text

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

Slide 97

Slide 97 text

Usage

Slide 98

Slide 98 text

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

Slide 99

Slide 99 text

Generated Code ↓ jump to definition

Slide 100

Slide 100 text

Association

Slide 101

Slide 101 text

Pu-Sub • Supported experimentally

Slide 102

Slide 102 text

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

Slide 103

Slide 103 text

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

Slide 104

Slide 104 text

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

Slide 105

Slide 105 text

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

Slide 106

Slide 106 text

Pros. and Cons. • SchemaDiffMigration is awesome

Slide 107

Slide 107 text

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

Slide 108

Slide 108 text

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

Slide 109

Slide 109 text

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

Slide 110

Slide 110 text

Any Questions?