A Room with a ViewModel

Investigating the Android Architecture Components

Architecture Components

  • Released in May as part of Google I|O 2017
  • Google's take on "the issues of the day"
  • Room: ORM-ish database access
  • Lifecycle: the Loader framework for a new era

Room: Entities

TL;DR: POJOs with Annotations

  • @Entity with metadata about names, indices, foreign keys, etc.
  • @PrimaryKey on the field that is unique
  • @Embedded to pull out some columns into separate nested object
  • @Ignore for things that Room should... y'know...

Room: DAO

Has Nothing To Do with Confucian Philosophy

  • @Dao to identify its role
  • @Query methods with SQL statements containing placeholders, returning entities or other POJOs
  • @Insert, @Update, @Delete methods for rest of entity CRUD
  • Use @Query for non-entity CRUD (delete by ID, delete all, etc.)

Room: RoomDatabase

A Database! For, Um, Room!

  • @Database annotation listing entity classes, version code
  • abstract methods returning DAO instances
  • Create instance using Room.databaseBuilder()

Room: Types

Wait? Everything Isn't a String?

  • Implement pair of methods, annotated with @TypeConverter, to round-trip convert from whatever you want into a simple type
  • Add @TypeConverters annotation to appropriate scope
    • Database
    • Entity
    • Field
    • Etc.

Room: Relations

OK, Now Things Get Weird

  • @ForeignKey on child, pointing to columns in parent and providing update/delete rules
  • And... that's it
    • No methods on parent to retrieve child
    • No methods on child to retrieve parent
    • Nothing in DAO to load object graph... except @Relation, which has issues
    • Argument: direct relations leads to accidental I/O on main application thread

Room: Migrations

Unless You Get the Schema Right on the First Try

  • Create instances of Migration
  • Implement migrate(), akin to onUpgrade() of SQLiteOpenHelper
  • Work with SQLite "directly", not via Room
  • Register migrations with RoomDatabase when building
  • Dedicated test support library

Room: Encryption

What? You Actually Expected Me Not To Talk About Security?

  • SupportSQLite... classes as abstraction around SQLite implementation
  • CWAC-SafeRoom: bridge between Room and SQLCipher for Android
  • Add dependency, create SafeHelperFactory with passphrase, add to RoomDatabase.Builder, and you're done

Lifecycles

The Story of Our Android Developer Lives

  • LifecycleOwner: Something that has a Lifecycle
    • LifecycleActivity
    • LifecycleFragment
    • ProcessLifecycleOwner: meta-lifecyle, aggregated for all activities
    • LifecycleService
    • Anything else you choose to teach about a Lifecycle

LiveData

Because UndeadData Was Too Creepy

  • Implements cut-down RxJava-style reactive system with observers
  • Lifecycle-aware, so will clean up after itself
  • Wrap @Query response in LiveData for reactive Room
    • ...though you can use RxJava Flowable, etc. too
    • Automatic reactive notification when data changes through Room
  • Can create your own LiveData for other data sources

ViewModel

Putting the VM in MVVM

  • Configuration-change-aware container of data
  • Designed to hold LiveData and other state for rendering your UI
  • In reality, thin (and strange) wrapper around retained fragments
  • Only works with FragmentActivity and backport Fragment

Where Things Go From Here

  • Expect slow progression through alpha and beta releases until 2017 Q4/2018 Q1
  • Expect more sample code from Google to use this stuff
  • Expect another pulse of activity in time for Google I|O 2018

Questions?

https://commonsware.com/presos/andevconDC2017