2. MVVM IN REAL LIFE
About me
Lea Cohen Tannoudji
B.Sc in Computational Biology at Hebrew
University
Android developer @ Lightricks
3. MVVM IN REAL LIFE
• 2013: Founded, 2019: Unicorn
Content creation products based on image &
video processing technology
• 200+ million downloads and counting
• 310 Employees
HQ in Israel, branches in the UK and Germany
Introducing Lightricks
6. MVVM IN REAL LIFE
MVVM
An architectural pattern that facilitates a
separation of GUI (View) from business
logic (Model).
ViewModel is a value converter,
responsible for exposing data objects
from the model for presenting.
7. MVVM IN REAL LIFE
MVVM - Why?
• Clear separation of responsibilities
• Maintainable
• Testable
• Allows reusing of Model
• Allows lean Views
8. MVVM IN REAL LIFE
Assumptions
• One ViewModel per Fragment/Activity
• ViewModel to View communications done
using LiveData
9. MVVM IN REAL LIFE
LiveData
An observable data holder class.
Lifecycle- aware, only updates app component
observers that are in active lifecycle state.
11. MVVM IN REAL LIFE
Lesson #1: Save state
Explicitly
VMs are stored in memory, and are designed
to survive configuration changes.
12. MVVM IN REAL LIFE
Lesson #1: Save state
Explicitly
They may NOT survive in cases of low memory
(killed by OS).
13. MVVM IN REAL LIFE
Save state Explicitly -
Example
14. Save state
Explicitly -
Solved
• ViewModel state
• Use onSaveInstanceState() (disk
persistent) for simple, lightweight
state.
• Save to storage (external internal )
or SQL database.
MVVM IN REAL LIFE
Persistability
15. Lesson #2: Multiple Vs.
Single LiveData Objects
Multiple LiveData objects, for each responsibility (i.e
toolbarLiveData, isProUserLiveData, previewLiveData).
Vs.
Single LiveData, containing all the data needed to
draw the fragment.
MVVM IN REAL LIFE
16. Single
LiveData -
Why ?
• Single source of truth
• Allows synchronous screen changes (i.e animations)
because Ui related logic is run on the main thread.
Note: calculating diff between old and new data need to be
efficient for this to work well.
As a rule: any stream should pass immutable objects since
changing an existing object is a source of bugs.
MVVM IN REAL LIFE
17. MVVM IN REAL LIFE
Lesson #3: Single
LiveData Events
LiveData can be used to tell the Fragment to
display data in some form (Toast, AlertDialog
etc.)
However, this data needs to be displayed
only once.
19. Single
LiveData
Events -
Solved
• Create SingleEventLiveData which
posts/sets every event only once
Or
• Wrap the event, allowing access only
if it hasn’t been handled
MVVM IN REAL LIFE
20. MVVM IN REAL LIFE
Conclusion
• Save VM state explicitly
• Consider using single LiveData for Ui updates
• Handling one time events using LiveData
21. Enlight Pixaloop Enlight Videoleap
MVVM IN REAL LIFE
Come build cool apps with us!
Enlight Photofox