Get a Room

https://commonsware.com/presos/devfestFL2017

All About the Annotations

  • @Entity models your tables
  • @DAO defines your data access API
  • @Database defines your, um, database

Relations

  • @ForeignKey declares your foreign keys (surprise!)
  • By default, that's it
    • No automatic retrieval of related objects
    • No collection accessors generated for you
    • Why? To make it easier to keep I/O on a background thread
  • @Relation used to kinda fake ORM APIs a bit

Type Massaging

  • @TypeConverter: convert objects to/from supported column types
  • @Embedded: convert batch of columns into an object, embedded in an entity or other object

Transactions

  • Default is typical: every DAO method is its own transaction
  • Use @Transaction
    • On a DAO method that calls other DAO methods
    • On a @Query that returns a @Relation
    • On a @Query that might return > 1MB of data

Threading

  • Default: everything is synchronous, you handle threading yourself
  • @Query Reactive Options
    • Return a LiveData or something from RxJava (Observable, Single, etc.)
    • Both return objects immediately, queries performed once you observe

Security

  • By default, everything stored in plaintext
  • CWAC-SafeRoom
    • Attaches SQLCipher for Android to Room
    • You supply passphrase, ideally Editable straight from EditText
    • One-liner plus a dependency to integrate

Questions?

https://commonsware.com/presos/devfestFL2017