Gotta Persist 'Em All: Realm as Replacement for SQLite

47
Gotta Persist ‘Em All: Realm as Replacement for SQLite Siena Aguayo Indiegogo @dotheastro

Transcript of Gotta Persist 'Em All: Realm as Replacement for SQLite

Gotta Persist ‘Em All: Realm as Replacement

for SQLite

Siena AguayoIndiegogo

@dotheastro

Overview

● What Realm Is

● Pros and cons

● Compare and contrast with SQLite

● What Realm Isn’t

But First, Hello!

Realm MeetupDrinkup + RoadmapMarch 24th, 2015San Francisco

What Realm Is

● “embedded mobile database”

Realm

● Core is written in C++

● Available for Android and iOS

● Second most-deployed mobile database in the world

Pros

Pros for Realm

● Very responsive team

● Easy to use

● Convenient for creating and storing data on the fly

● Object conversion handled for you

● Faster than SQLite

Source: https://realm.io/news/realm-for-android/#realm-for-android

Source: https://realm.io/news/realm-for-android/#realm-for-android

Source: https://realm.io/news/realm-for-android/#realm-for-android

Cons

Cons for Realm

● Have to create with Java on the device

● Still under active development

● Not a lot of content online

● No importing

● Can’t access objects across threads

Missing Features

● Auto-incrementing ids

● Easy migrations (exist, but are painful)

● Compound primary keys

● Notifications on specific data changed

● Null support

● Testing with Robolectric

● Map<K, V> support

Compare and Contrast with SQLite

Database Connectionand Setup

Shipping with Existing Database

copy

SQLite: SQLiteAssetHelperRealm: Sample migration app

Shipping with Existing Database

SQLite: dbHelper.getReadableDatabase() ordbHelper.getWritableDatabase()

Realm: Realm.getInstance(context)

Creating Schema

SQLite

Creating Schema

Realm

No args constructor

Completely vanilla getters and setters

NO custom logic in your models

In Realm, your models ARE your schema.

Reading Data

Java with SQLite

Find All

Java with Realm

Find First

Writing Data

Java with SQLite

Java with Realm

Java with Realm

Adding Relationships andComplex Queries

Encounter has a LocationAreaLocationArea has a Location

LocationLocationAreaEncounter

Raw SQL

Java with Realm

What Realm Isn’t

Realm says they are not an ORM because your data is not copied.

An ORM or not?

SQLite>> pragma table_info(pokemon)0|id|INTEGER|1||11|identifier|VARCHAR(79)|1||02|height|INTEGER|1||03|weight|INTEGER|1||0

Javapublic class Pokemon { private int id; private String identifier; private int height; private int weight; // constructor(s), // getters, setters}

Typical ORM usage

Data from Realm in the toString()...

Member variables are all uninitialized...

Shout-Out to Other ORMs

● greenDAO● Active Android● Sugar ORM● ORMLite (Java)● Cupboard

Michael Pardo’s Android Data talk

Conclusion

Acknowledgments

● Realm: http://realm.io/ ● SQLite db from veekun’s Pokémon repo:

https://github.com/veekun/pokedex

Questions?

Twitter: @dotheastro