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.

Ee5d76ef07f4e8ac107ebde8c104f3b7?s=128

Siena Aguayo

May 05, 2016
Tweet

Transcript

  1. Gotta Persist ‘Em All: Realm as Replacement for SQLite Siena

    Aguayo Indiegogo @sienatime
  2. Overview • What Realm Is • Pros and cons •

    Compare and contrast with SQLite • What Realm Isn’t
  3. But First, Hello!

  4. None
  5. None
  6. None
  7. What Realm Is

  8. • “embedded mobile database” Realm • Core is written in

    C++ • Available for Android and iOS • Second most-deployed mobile database in the world
  9. Pros

  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
  11. Source: http://www.slideshare.net/ChristianMelchior/realm-building-a-mobile-database#25

  12. Cons

  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
  14. Missing Features • Auto-incrementing ids • Compound primary keys •

    Testing with Robolectric • Map<K, V> support
  15. Compare and Contrast with SQLite

  16. Database Connection and Setup

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

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

  19. Creating Schema SQLite

  20. Creating Schema Realm

  21. In Realm, your models ARE your schema.

  22. Reading Data

  23. Java with SQLite

  24. Find All Java with Realm Find First

  25. Writing Data

  26. Java with SQLite

  27. Java with Realm

  28. Java with Realm

  29. Adding Relationships and Complex Queries

  30. Encounter has a LocationArea LocationArea has a Location

  31. Location LocationArea Encounter

  32. Raw SQL

  33. Java with Realm

  34. None
  35. What Realm Isn’t

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

    is not copied.
  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
  38. Data from Realm in the toString()... Member variables are all

    uninitialized...
  39. Shout-Out to Other ORMs • greenDAO • Active Android •

    Sugar ORM • ORMLite (Java) • Cupboard Michael Pardo’s Android Data talk
  40. Conclusion

  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
  42. Questions? Twitter: @sienatime