$30 off During Our Annual Pro Sale. View Details »

Gotta Persist 'Em All: Realm as Replacement for SQLite

Gotta Persist 'Em All: Realm as Replacement for SQLite

Realm is a new player on mobile database scene, and acts as a replacement for SQLite on Android and Core Data on iOS by providing a database and an easy-to-use object-relational mapping-like interface. Learn how to use it in your own projects and how it compares to working with SQLite in a beginner-friendly sample app with Pokémon.

Siena Aguayo

May 05, 2016
Tweet

More Decks by Siena Aguayo

Other Decks in Technology

Transcript

  1. Gotta Persist ‘Em All:
    Realm as Replacement
    for SQLite
    Siena Aguayo
    Indiegogo
    @sienatime

    View Slide

  2. Overview
    ● What Realm Is
    ● Pros and cons
    ● Compare and contrast with SQLite
    ● What Realm Isn’t

    View Slide

  3. But First, Hello!

    View Slide

  4. View Slide

  5. View Slide

  6. View Slide

  7. What Realm Is

    View Slide

  8. ● “embedded mobile database”
    Realm
    ● Core is written in C++
    ● Available for Android and iOS
    ● Second most-deployed mobile database in
    the world

    View Slide

  9. Pros

    View Slide

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

    View Slide

  11. Source: http://www.slideshare.net/ChristianMelchior/realm-building-a-mobile-database#25

    View Slide

  12. Cons

    View Slide

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

    View Slide

  14. Missing Features
    ● Auto-incrementing ids
    ● Compound primary keys
    ● Testing with Robolectric
    ● Map support

    View Slide

  15. Compare and Contrast with SQLite

    View Slide

  16. Database Connection
    and Setup

    View Slide

  17. Shipping with Existing Database
    copy
    SQLite: SQLiteAssetHelper
    Realm: Sample migration app

    View Slide

  18. Shipping with Existing Database
    SQLite:
    dbHelper.getReadableDatabase() or
    dbHelper.getWritableDatabase()
    Realm:
    Realm.getDefaultInstance()

    View Slide

  19. Creating Schema
    SQLite

    View Slide

  20. Creating Schema
    Realm

    View Slide

  21. In Realm, your models ARE your schema.

    View Slide

  22. Reading Data

    View Slide

  23. Java with SQLite

    View Slide

  24. Find All
    Java with Realm
    Find First

    View Slide

  25. Writing Data

    View Slide

  26. Java with SQLite

    View Slide

  27. Java with Realm

    View Slide

  28. Java with Realm

    View Slide

  29. Adding Relationships and
    Complex Queries

    View Slide

  30. Encounter has a LocationArea
    LocationArea has a Location

    View Slide

  31. Location
    LocationArea
    Encounter

    View Slide

  32. Raw SQL

    View Slide

  33. Java with Realm

    View Slide

  34. View Slide

  35. What Realm Isn’t

    View Slide

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

    View Slide

  37. An ORM or not?
    SQLite
    >> pragma table_info(pokemon)
    0|id|INTEGER|1||1
    1|identifier|VARCHAR(79)|1||0
    2|height|INTEGER|1||0
    3|weight|INTEGER|1||0
    Java
    public class Pokemon {
    private int id;
    private String identifier;
    private int height;
    private int weight;
    // constructor(s),
    // getters, setters
    }
    Typical ORM usage

    View Slide

  38. Data from Realm in the toString()...
    Member variables are all uninitialized...

    View Slide

  39. Shout-Out to Other ORMs
    ● greenDAO
    ● Active Android
    ● Sugar ORM
    ● ORMLite (Java)
    ● Cupboard
    Michael Pardo’s Android Data talk

    View Slide

  40. Conclusion

    View Slide

  41. Acknowledgments
    ● Realm: http://realm.io/
    ● SQLite db from veekun’s Pokémon repo:
    https://github.com/veekun/pokedex
    ● Official Pokémon art by Ken Sugimori
    ● Christian Melchior for feedback

    View Slide

  42. Questions?
    Twitter: @sienatime

    View Slide