Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Gotta Persist 'Em All: Realm as Replacement for SQLite

8,769 views

Published on

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 (ORM) 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. This talk was presented at Droidcon Montreal on April 10th, 2015.

Published in: Software
  • Be the first to comment

Gotta Persist 'Em All: Realm as Replacement for SQLite

  1. 1. Gotta Persist ‘Em All: Realm as Replacement for SQLite Siena Aguayo Indiegogo @dotheastro
  2. 2. Overview ● What Realm Is ● Pros and cons ● Compare and contrast with SQLite ● What Realm Isn’t
  3. 3. But First, Hello!
  4. 4. Realm Meetup Drinkup + Roadmap March 24th, 2015 San Francisco
  5. 5. What Realm Is
  6. 6. ● “embedded mobile database” Realm ● Core is written in C++ ● Available for Android and iOS ● Second most-deployed mobile database in the world
  7. 7. Pros
  8. 8. 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
  9. 9. Source: https://realm.io/news/realm-for-android/#realm-for-android
  10. 10. Source: https://realm.io/news/realm-for-android/#realm-for-android
  11. 11. Source: https://realm.io/news/realm-for-android/#realm-for-android
  12. 12. Cons
  13. 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. 14. 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
  15. 15. Compare and Contrast with SQLite
  16. 16. Database Connection and Setup
  17. 17. Shipping with Existing Database copy SQLite: SQLiteAssetHelper Realm: Sample migration app
  18. 18. Shipping with Existing Database SQLite: dbHelper.getReadableDatabase() or dbHelper.getWritableDatabase() Realm: Realm.getInstance(context)
  19. 19. Creating Schema SQLite
  20. 20. Creating Schema Realm
  21. 21. No args constructor Completely vanilla getters and setters NO custom logic in your models
  22. 22. In Realm, your models ARE your schema.
  23. 23. Reading Data
  24. 24. Java with SQLite
  25. 25. Find All Java with Realm Find First
  26. 26. Writing Data
  27. 27. Java with SQLite
  28. 28. Java with Realm
  29. 29. Java with Realm
  30. 30. Adding Relationships and Complex Queries
  31. 31. Encounter has a LocationArea LocationArea has a Location
  32. 32. Location LocationAreaEncounter
  33. 33. Raw SQL
  34. 34. Java with Realm
  35. 35. What Realm Isn’t
  36. 36. Realm says they are not an ORM because your data is not copied.
  37. 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. 38. Data from Realm in the toString()... Member variables are all uninitialized...
  39. 39. Shout-Out to Other ORMs ● greenDAO ● Active Android ● Sugar ORM ● ORMLite (Java) ● Cupboard Michael Pardo’s Android Data talk
  40. 40. Conclusion
  41. 41. Acknowledgments ● Realm: http://realm.io/ ● SQLite db from veekun’s Pokémon repo: https://github.com/veekun/pokedex
  42. 42. Questions? Twitter: @dotheastro

×