Fun with LiveData (Android Dev Summit '18)

Android Developers
7 Nov 201819:20

Summary

TLDRIn this talk, Jose Alcerreca and Yigit Boyar from Google discuss the concept of LiveData in Android development. They explain its role as a lifecycle-aware observable data holder, its benefits in managing UI updates, and how it avoids common issues like memory leaks. The speakers cover how to combine and transform LiveData using functions like map and switchMap, while also addressing anti-patterns and mistakes developers might make. They emphasize that LiveData is best suited for UI-related tasks and suggest alternatives for non-UI use cases, such as RxJava or coroutines for complex operations.

Takeaways

  • 😀 LiveData is a lifecycle-aware observable data holder that helps communicate between ViewModels and UI components without causing memory leaks.
  • 😀 LiveData automatically manages subscriptions based on the lifecycle state of observers, preventing unnecessary callbacks when the activity or fragment is destroyed.
  • 😀 Unlike streams, LiveData holds only the latest value, which is ideal for UI data but may not be suitable for processing streams of events.
  • 😀 The two main transformations provided by LiveData are map and switchMap, which allow you to transform data or chain LiveDatas together.
  • 😀 The map transformation is a one-to-one static transformation, perfect for cases where you need to convert a data model to a UI model.
  • 😀 switchMap allows you to transform a LiveData of one type (e.g., user ID) into another LiveData (e.g., user data) and handles switching between them.
  • 😀 Using mediator LiveData allows you to create custom transformations by combining multiple LiveDatas and triggering actions when their values change.
  • 😀 LiveData is not suitable for large or one-time operations like converting JSON to objects. For such cases, you should use one-shot operations instead.
  • 😀 When sharing LiveData between components like a repository and multiple observers, ensure that new LiveData instances are created to avoid glitches during activity transitions.
  • 😀 Avoid exposing mutable LiveData from ViewModels, and instead use immutable LiveData to prevent unintentional changes and ensure proper data flow.
  • 😀 For non-UI related tasks or complex reactive programming scenarios, consider using RxJava or coroutines, as LiveData is best suited for UI-related, lifecycle-aware operations.

Q & A

  • What is LiveData and how is it different from regular data holders in Android?

    -LiveData is a lifecycle-aware observable data holder. Unlike regular data holders, it automatically manages its subscription based on the lifecycle state of the observer (such as an activity or fragment), ensuring that the data is only delivered when the observer is in a valid state, thus avoiding memory leaks or crashes.

  • Why should you avoid keeping references to activities in the ViewModel?

    -Keeping references to activities in the ViewModel can cause memory leaks and crashes due to the activity being recreated on events like device rotation. It's better to use LiveData, which allows the activity to observe data without direct references.

  • What does it mean that LiveData is lifecycle-aware?

    -Being lifecycle-aware means that LiveData automatically manages the subscription for you. It ensures that observers (like activities or fragments) receive updates only when they are in a valid lifecycle state (e.g., started, resumed). If the observer's lifecycle is not active, LiveData does not call the observer back.

  • What happens when you set a new value to LiveData while the observer is not active?

    -If you set a new value to LiveData when the observer is not in an active state (e.g., when the activity is in the background), the new value is not dispatched to the observer. However, when the observer becomes active again, it will receive the latest value.

  • How do you combine multiple LiveData sources in a ViewModel?

    -To combine multiple LiveDatas, you can use transformations like `map` or `switchMap`. These functions allow you to transform the data from one LiveData into another or combine multiple LiveDatas into a single output, while still respecting lifecycle awareness.

  • What is the difference between `map` and `switchMap` in LiveData transformations?

    -`map` transforms data from one LiveData to another without affecting its lifecycle. It's a one-to-one static transformation. On the other hand, `switchMap` is used when you need to switch between LiveDatas, like when one LiveData triggers the creation of another LiveData. It automatically unsubscribes from the old LiveData and subscribes to the new one whenever the source LiveData changes.

  • Why is it important to create a new LiveData instance every time in a repository?

    -Creating a new LiveData instance ensures that data is scoped correctly for each activity or fragment. Sharing a single LiveData instance across multiple components can lead to unintended behavior, like an activity receiving data meant for another activity, which can cause glitches or other issues.

  • What is a common mistake when using LiveData for web requests or large data processing?

    -A common mistake is using LiveData to hold large data objects, such as the result of a web request or large JSON responses. Since LiveData is a value holder, it will keep the data in memory, potentially leading to high memory usage or performance issues. It's better to handle such data as a one-shot operation and avoid keeping large objects in LiveData.

  • What should you do to avoid issues when creating transformations in LiveData?

    -When creating transformations, ensure you plan the lifecycle and data flow carefully. Avoid reassignment of LiveData after it has been observed, as this can cause the observer to miss updates. Also, ensure transformations are set up using immutable LiveData to avoid accidental changes.

  • When should you use LiveData and when should you use RxJava or coroutines instead?

    -Use LiveData for scenarios involving lifecycle-aware UI components, such as observing data for the UI layer. For complex event streams, operations that don't involve lifecycle (e.g., background tasks), or when you need more control over streams, use RxJava or coroutines instead. LiveData is not designed for heavy stream processing or non-UI-related operations.

Outlines

plate

Cette section est réservée aux utilisateurs payants. Améliorez votre compte pour accéder à cette section.

Améliorer maintenant

Mindmap

plate

Cette section est réservée aux utilisateurs payants. Améliorez votre compte pour accéder à cette section.

Améliorer maintenant

Keywords

plate

Cette section est réservée aux utilisateurs payants. Améliorez votre compte pour accéder à cette section.

Améliorer maintenant

Highlights

plate

Cette section est réservée aux utilisateurs payants. Améliorez votre compte pour accéder à cette section.

Améliorer maintenant

Transcripts

plate

Cette section est réservée aux utilisateurs payants. Améliorez votre compte pour accéder à cette section.

Améliorer maintenant
Rate This

5.0 / 5 (0 votes)

Étiquettes Connexes
LiveDataAndroid DevelopmentLifecycle AwarenessViewModelTransformationsData BindingDeveloper TalkBest PracticesUI DesignGoogle DevelopersAndroid Toolkit
Besoin d'un résumé en anglais ?