Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Android Architecture Components Room, LiveData ...

Android Architecture Components Room, LiveData & ViewModel

Sesión del mes de Agosto del MeetUp de Medellín Android, abordando los nuevos Android Architecture Components, mostrando tanto el detalle técnico como el de implementación con un ejemplo funcional.
These slides correspond to Medellin Android's August session, tackling new Android Architecture Components and showing their technical details as well as implementation ones with a functional example.

Carlos Daniel

August 09, 2017
Tweet

More Decks by Carlos Daniel

Other Decks in Programming

Transcript

  1. 6 Android Development Process • Put all your code into

    Activities files • Occasionally, move some stuff into AsyncTasks if error NetworkOnMainThreadException
  2. 7 Android Development Process • Put all your code into

    Activities files • Occasionally, move some stuff into AsyncTasks if error NetworkOnMainThreadException • Unit tests and instrumentation tests → hard to do, read and maintain
  3. 8 Evolution • Patterns: ◦ MVC ◦ MVP ◦ MVVM

    ◦ . ◦ . ◦ . • 3rd party Libraries
  4. 16 What are AAC framework? • Libraries • Guidelines •

    Common scenarios in apps dev • Aims:
  5. 17 What are AAC framework? • Libraries • Guidelines •

    Common scenarios in apps dev • Aims: ◦ Reduce boilerplate
  6. 18 What are AAC framework? • Libraries • Guidelines •

    Common scenarios in apps dev • Aims: ◦ Reduce boilerplate ◦ Reduce repetitive code
  7. 19 What are AAC framework? • Libraries • Guidelines •

    Common scenarios in apps dev • Aims: ◦ Reduce boilerplate ◦ Reduce repetitive code ◦ Focus on core features
  8. 22 Basic blocks of AAC • Room → A SQLite

    object mapper (ORMLite - GreenDao)
  9. 23 Basic blocks of AAC • Room → A SQLite

    object mapper (ORMLite - GreenDao) • LiveData
  10. 24 Basic blocks of AAC • Room → A SQLite

    object mapper (ORMLite - GreenDao) • LiveData → A Lifecycle aware observable core component.
  11. 25 Basic blocks of AAC • Room → A SQLite

    object mapper (ORMLite - GreenDao) • LiveData → A Lifecycle aware observable core component. • ViewModel
  12. 26 Basic blocks of AAC • Room → A SQLite

    object mapper (ORMLite - GreenDao) • LiveData → A Lifecycle aware observable core component. • ViewModel → The communication points with the rest of the application for Activities / Fragments. They are UI code free and outlive the activity or fragment.
  13. 27 Basic blocks of AAC • Room → A SQLite

    object mapper (ORMLite - GreenDao) • LiveData → A Lifecycle aware observable core component. • ViewModel → The communication points with the rest of the application for Activities / Fragments. They are UI code free and outlive the activity or fragment. • Lifecycle
  14. 28 Basic blocks of AAC • Room → A SQLite

    object mapper (ORMLite - GreenDao) • LiveData → A Lifecycle aware observable core component. • ViewModel → The communication points with the rest of the application for Activities / Fragments. They are UI code free and outlive the activity or fragment. • Lifecycle → contains information about the lifecycle state of a component (for instance an Activity).
  15. 29 Basic blocks of AAC • Room → A SQLite

    object mapper (ORMLite - GreenDao) • LiveData → A Lifecycle aware observable core component. • ViewModel → The communication points with the rest of the application for Activities / Fragments. They are UI code free and outlive the activity or fragment. • Lifecycle → contains information about the lifecycle state of a component (for instance an Activity). • Lifecycle owner
  16. 30 Basic blocks of AAC • Room → A SQLite

    object mapper (ORMLite - GreenDao) • LiveData → A Lifecycle aware observable core component. • ViewModel → The communication points with the rest of the application for Activities / Fragments. They are UI code free and outlive the activity or fragment. • Lifecycle → contains information about the lifecycle state of a component (for instance an Activity). • Lifecycle owner → Interface - Annotations @OnLifecycleEvent(Lifecycle.Event.ON_START)
  17. 31 Basic blocks of AAC • Room → A SQLite

    object mapper (ORMLite - GreenDao) • LiveData → A Lifecycle aware observable core component. • ViewModel → The communication points with the rest of the application for Activities / Fragments. They are UI code free and outlive the activity or fragment. • Lifecycle → contains information about the lifecycle state of a component (for instance an Activity). • Lifecycle owner → Interface - Annotations @OnLifecycleEvent(Lifecycle.Event.ON_START) • Lifecycle observer
  18. 32 Basic blocks of AAC • Room → A SQLite

    object mapper (ORMLite - GreenDao) • LiveData → A Lifecycle aware observable core component. • ViewModel → The communication points with the rest of the application for Activities / Fragments. They are UI code free and outlive the activity or fragment. • Lifecycle → contains information about the lifecycle state of a component (for instance an Activity). • Lifecycle owner → Interface - Annotations @OnLifecycleEvent(Lifecycle.Event.ON_START) • Lifecycle observer → Interface - getLifecycle(): LifeCycleRegistry
  19. Android Architecture Components - AAC Can be used in isolation.

    Work really well when used together, though.
  20. 38 Using Architecture Components • App → Events Scheduler ◦

    Event addition ◦ Event list • Initial Version → Offline
  21. 39 Using Architecture Components • App → Events Scheduler ◦

    Event addition ◦ Event list • Initial Version → Offline • Architecture Components: ◦ Lifecycle ◦ ViewModel ◦ Room
  22. 40 MVVM using Architecture Components EventRepository EventDatabase EventDao Events Event

    List EventListActivity EventListViewModel Add Event AddEventActivity AddEventViewModel extends ViewModel @Database (Room) @Dao (Room) extends Lifecycle
  23. 41 What is Room? • New way to create databases

    in Android • ORM between Java and SQLite • Less verbose • No more Cursors • No more Loaders • Room isn’t a fully-fledged ORM (no complex nesting objects)
  24. 43 How to query data? • Using LiveData → exposes

    a stream of events that we can subscribe to receive updates for asynchronously
  25. 44 How to query data? • Using LiveData → exposes

    a stream of events that we can subscribe to receive updates for asynchronously • Using RxJava2 (Flowable)
  26. 45 How to query data? • Using LiveData → exposes

    a stream of events that we can subscribe to receive updates for asynchronously • Using RxJava2 (Flowable) • Place synchronous calls in a background thread such as an AsyncTask. (Room doesn’t allow to issue database queries on the main thread → ANRs)
  27. 46 Looking at ViewModels • ViewModel name comes from MVVM

    pattern • Responsible for preparing data for the view • Expose data to any view listening for changes • Retain state across activity changes → No onSaveInstanceState() • Share data across activities/fragments → No actions • Stay in memory until Lifecycle is scoped ◦ Activity finishes / Fragment detached • Should not reference any View → Memory leak • Extend AndroidViewModel to access app context
  28. 48 How to work with AAC? • Empty Activity project

    • Add Google Maven Repo en project’s build.gradle • Add AAC’s components to app/build.gradle • Let’s code! :)
  29. 49 References • Android Architecture Components - https://developer.android.com/topic/libraries/architecture/index.html • Android

    Lifecycles - https://developer.android.com/topic/libraries/architecture/lifecycle.html • Room Reference - https://developer.android.com/topic/libraries/architecture/room.html • LiveData Reference - https://developer.android.com/topic/libraries/architecture/livedata.html • ViewModel Reference - https://developer.android.com/topic/libraries/architecture/viewmodel.html
  30. 50 References • Android Persistence Codelab - https://codelabs.developers.google.com/codelabs/android-persistence/#0 • Google’s

    Android Architecture Github - https://github.com/googlesamples/android-architecture • Project shown in this MeetUp session - cdmunoz - https://github.com/cdmunoz/EventSchedulerArchComp • Simple GoogleBooks MVVM project - cdmunoz - https://github.com/cdmunoz/GBooksViewModel