Skip to content

Instantly share code, notes, and snippets.

@humblehacker
Last active October 4, 2024 19:17

Revisions

  1. humblehacker revised this gist Dec 26, 2018. 1 changed file with 31 additions and 17 deletions.
    48 changes: 31 additions & 17 deletions mutablelivedata.md
    Original file line number Diff line number Diff line change
    @@ -10,36 +10,50 @@

    ### Transformation Setup

    The third item is about where and when to create your transformations and this is all about wiring. It's similar to when you create a circuit. You lay down your components and you wire everything up. And for a known set of inputs you're going to have a known set of outputs. You don't unplug a wire while it's in operation and plug it somewhere else, right?

    This is exactly what this view model is doing. Lots of horrible things happening in this view model, by the way. For starters ...
    > The third item is about where and when to create your transformations and this is all about wiring. It's similar to when you create a circuit. You lay down your components and you wire everything up. And for a known set of inputs you're going to have a known set of outputs. You don't unplug a wire while it's in operation and plug it somewhere else, right?
    >
    > This is exactly what this view model is doing. Lots of horrible things happening in this view model, by the way. For starters ...
    | Don't do this |
    |--|
    | ![screen shot 2018-12-21 at 9 31 03 am](https://user-images.githubusercontent.com/117582/50355359-46a88200-0503-11e9-95f6-bee1f32a923e.png) |

    B: You should [show] "don't do this" on this slide

    A: It says "don't do this", literally.

    B: I'm sure someone will copy/paste it and blame us for recommending it.

    A: That's the standard way of doing it.

    A: So first, it's exposing `itemData`, which is a variable. It's a [var] not a [val]. And also, it's exposing a `MutableLiveData`. Almost ... You should almost never do this. Two-way DataBinding is an exception to this, maybe. You should always expose something that is immutable so your observers can't change it.
    > B: You should [show] "don't do this" on this slide
    >
    > A: It says "don't do this", literally.
    >
    > B: I'm sure someone will copy/paste it and blame us for recommending it.
    >
    > A: That's the standard way of doing it.
    >
    > A: So first, it's exposing `itemData`, which is a variable. It's a [var] not a [val]. And also, it's exposing a `MutableLiveData`. Almost ... You should almost never do this. Two-way DataBinding is an exception to this, maybe. You should always expose something that is immutable so your observers can't change it.
    ...

    ## [Example of two-way DataBinding](https://youtu.be/T-nQP9fidKU?t=1324)

    And because we are using this property for two-way DataBinding, we need to expose it as `MutableLiveData`, because otherwise the layout won't be able to access the `value` property of `MutableLiveData`. Which is sad that we really need to expose `MutableLiveData`, but hey, if it's gonna save us some time and effort, why not? However, if you're aren't using two-way DataBinding, I would _always_ suggest that you don't expose `MutableLiveData` directly, but that you only expose `LiveData` to outside classes because that's just going to make your code much much safer.
    > And because we are using this property for two-way DataBinding, we need to expose it as `MutableLiveData`, because otherwise the layout won't be able to access the `value` property of `MutableLiveData`. Which is sad that we really need to expose `MutableLiveData`, but hey, if it's gonna save us some time and effort, why not? However, if you're aren't using two-way DataBinding, I would _always_ suggest that you don't expose `MutableLiveData` directly, but that you only expose `LiveData` to outside classes because that's just going to make your code much much safer.
    ## [Lifecycle, LiveData, ViewModels - The inner wiring by Florina Muntenescu, Google EN](https://youtu.be/U6Lgym1XEBI?t=788)

    So, the class that would help you set these values to the LiveData is the MutableLiveData. You would use the postValue method to set the values on a background thread and setValue when you're setting the values from the main thread.

    So, especially if you're using Kotlin, something that I don't like its to expose this Mutable object to the outside of the class. So what I prefer to do is to expose the LiveData object and use a backing field that is a MutableLiveData. So like this, I ensure that only the class that I'm working in can change the LiveData, so I'm still keeping this immutability principle.
    > So, the class that would help you set these values to the LiveData is the MutableLiveData. You would use the postValue method to set the values on a background thread and setValue when you're setting the values from the main thread.
    >
    > So, especially if you're using Kotlin, something that I don't like its to expose this Mutable object to the outside of the class. So what I prefer to do is to expose the LiveData object and use a backing field that is a MutableLiveData. So like this, I ensure that only the class that I'm working in can change the LiveData, so I'm still keeping this immutability principle.
    | Don't do this | Do this |
    |--|--|
    | ![screen shot 2018-12-21 at 9 56 21 am](https://user-images.githubusercontent.com/117582/50356462-dac81880-0506-11e9-939a-eeec04cdf8b2.png) | ![screen shot 2018-12-21 at 9 56 41 am](https://user-images.githubusercontent.com/117582/50356465-de5b9f80-0506-11e9-9e0b-1ade72164825.png) |
    | ![screen shot 2018-12-21 at 9 56 21 am](https://user-images.githubusercontent.com/117582/50356462-dac81880-0506-11e9-939a-eeec04cdf8b2.png) | ![screen shot 2018-12-21 at 9 56 41 am](https://user-images.githubusercontent.com/117582/50356465-de5b9f80-0506-11e9-9e0b-1ade72164825.png) |
    | | |

    ## [droidcon NYC 2017 - ViewModels, LiveData and Lifecycles, oh my!](https://youtu.be/SlZVYkhoSq8?t=1290)

    | Don't publicly expose `MutableLiveData` |
    |--|
    | ![screen shot 2018-12-21 at 12 03 17 pm](https://user-images.githubusercontent.com/117582/50361459-6f874200-0518-11e9-9562-d19043662cd8.png) |

    > So you might have noticed that I've been using both this class called `MutableLiveData` and `LiveData`. So `MutableLiveData`, as the name implies, is `LiveData` that could be changed. And what that means is that it exposes the `setValue` and `postValue` methods. `setValue` and `postValue` are just a little bit different. `setValue` is meant for being called on the main thread and then `postValue` can be called from a background thread.
    >
    > The actual `LiveData` class is sort of read-only in that you can't call, you can't change what's it using `setValue` and `postValue`.
    > A general rule of thumb is that you'll only ever publicly expose `LiveData` outside of the view model. Whereas inside of the view model you could use that `MutableLiveData`. So by using this encapsulation you're kind of ensuring that the view model is always the one that's doing all the editing and processing of your `LiveData`, and everything outside of there is just observing the `LiveData`.
  2. humblehacker revised this gist Dec 21, 2018. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion mutablelivedata.md
    Original file line number Diff line number Diff line change
    @@ -42,4 +42,4 @@ So, especially if you're using Kotlin, something that I don't like its to expose

    | Don't do this | Do this |
    |--|--|
    | | |
    | ![screen shot 2018-12-21 at 9 56 21 am](https://user-images.githubusercontent.com/117582/50356462-dac81880-0506-11e9-939a-eeec04cdf8b2.png) | ![screen shot 2018-12-21 at 9 56 41 am](https://user-images.githubusercontent.com/117582/50356465-de5b9f80-0506-11e9-9e0b-1ade72164825.png) |
  3. humblehacker revised this gist Dec 21, 2018. 1 changed file with 15 additions and 1 deletion.
    16 changes: 15 additions & 1 deletion mutablelivedata.md
    Original file line number Diff line number Diff line change
    @@ -28,4 +28,18 @@ A: That's the standard way of doing it.

    A: So first, it's exposing `itemData`, which is a variable. It's a [var] not a [val]. And also, it's exposing a `MutableLiveData`. Almost ... You should almost never do this. Two-way DataBinding is an exception to this, maybe. You should always expose something that is immutable so your observers can't change it.

    ...
    ...

    ## [Example of two-way DataBinding](https://youtu.be/T-nQP9fidKU?t=1324)

    And because we are using this property for two-way DataBinding, we need to expose it as `MutableLiveData`, because otherwise the layout won't be able to access the `value` property of `MutableLiveData`. Which is sad that we really need to expose `MutableLiveData`, but hey, if it's gonna save us some time and effort, why not? However, if you're aren't using two-way DataBinding, I would _always_ suggest that you don't expose `MutableLiveData` directly, but that you only expose `LiveData` to outside classes because that's just going to make your code much much safer.

    ## [Lifecycle, LiveData, ViewModels - The inner wiring by Florina Muntenescu, Google EN](https://youtu.be/U6Lgym1XEBI?t=788)

    So, the class that would help you set these values to the LiveData is the MutableLiveData. You would use the postValue method to set the values on a background thread and setValue when you're setting the values from the main thread.

    So, especially if you're using Kotlin, something that I don't like its to expose this Mutable object to the outside of the class. So what I prefer to do is to expose the LiveData object and use a backing field that is a MutableLiveData. So like this, I ensure that only the class that I'm working in can change the LiveData, so I'm still keeping this immutability principle.

    | Don't do this | Do this |
    |--|--|
    | | |
  4. humblehacker revised this gist Dec 21, 2018. 1 changed file with 8 additions and 2 deletions.
    10 changes: 8 additions & 2 deletions mutablelivedata.md
    Original file line number Diff line number Diff line change
    @@ -2,15 +2,21 @@

    ## Example from [Android Jetpack: LiveData](https://youtu.be/OMcDk2_4LSk?t=65)

    ![MutableLiveData Example](https://user-images.githubusercontent.com/117582/50354468-815ceb00-0500-11e9-82ae-571e17ce7ebe.png)
    | MutableLiveData Example |
    |--|
    | ![MutableLiveData Example](https://user-images.githubusercontent.com/117582/50354468-815ceb00-0500-11e9-82ae-571e17ce7ebe.png) |

    ## [Fun with LiveData (Android Dev Summit '18)](https://youtu.be/2rO4r-JOQtA?t=855)

    ### Transformation Setup

    The third item is about where and when to create your transformations and this is all about wiring. It's similar to when you create a circuit. You lay down your components and you wire everything up. And for a known set of inputs you're going to have a known set of outputs. You don't unplug a wire while it's in operation and plug it somewhere else, right?

    This is exactly what this view model is doing. Lots of horrible things happening in this view model, by the way. For starters ...

    ![screen shot 2018-12-21 at 9 31 03 am](https://user-images.githubusercontent.com/117582/50355359-46a88200-0503-11e9-95f6-bee1f32a923e.png)
    | Don't do this |
    |--|
    | ![screen shot 2018-12-21 at 9 31 03 am](https://user-images.githubusercontent.com/117582/50355359-46a88200-0503-11e9-95f6-bee1f32a923e.png) |

    B: You should [show] "don't do this" on this slide

  5. humblehacker revised this gist Dec 21, 2018. 1 changed file with 2 additions and 0 deletions.
    2 changes: 2 additions & 0 deletions mutablelivedata.md
    Original file line number Diff line number Diff line change
    @@ -10,6 +10,8 @@ The third item is about where and when to create your transformations and this i

    This is exactly what this view model is doing. Lots of horrible things happening in this view model, by the way. For starters ...

    ![screen shot 2018-12-21 at 9 31 03 am](https://user-images.githubusercontent.com/117582/50355359-46a88200-0503-11e9-95f6-bee1f32a923e.png)

    B: You should [show] "don't do this" on this slide

    A: It says "don't do this", literally.
  6. humblehacker revised this gist Dec 21, 2018. 1 changed file with 19 additions and 1 deletion.
    20 changes: 19 additions & 1 deletion mutablelivedata.md
    Original file line number Diff line number Diff line change
    @@ -1,5 +1,23 @@
    # Don't expose `MutableLiveData`

    Example from [Android Jetpack: LiveData](https://youtu.be/OMcDk2_4LSk?t=65)
    ## Example from [Android Jetpack: LiveData](https://youtu.be/OMcDk2_4LSk?t=65)

    ![MutableLiveData Example](https://user-images.githubusercontent.com/117582/50354468-815ceb00-0500-11e9-82ae-571e17ce7ebe.png)

    ## [Fun with LiveData (Android Dev Summit '18)](https://youtu.be/2rO4r-JOQtA?t=855)

    The third item is about where and when to create your transformations and this is all about wiring. It's similar to when you create a circuit. You lay down your components and you wire everything up. And for a known set of inputs you're going to have a known set of outputs. You don't unplug a wire while it's in operation and plug it somewhere else, right?

    This is exactly what this view model is doing. Lots of horrible things happening in this view model, by the way. For starters ...

    B: You should [show] "don't do this" on this slide

    A: It says "don't do this", literally.

    B: I'm sure someone will copy/paste it and blame us for recommending it.

    A: That's the standard way of doing it.

    A: So first, it's exposing `itemData`, which is a variable. It's a [var] not a [val]. And also, it's exposing a `MutableLiveData`. Almost ... You should almost never do this. Two-way DataBinding is an exception to this, maybe. You should always expose something that is immutable so your observers can't change it.

    ...
  7. humblehacker revised this gist Dec 21, 2018. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion mutablelivedata.md
    Original file line number Diff line number Diff line change
    @@ -2,4 +2,4 @@

    Example from [Android Jetpack: LiveData](https://youtu.be/OMcDk2_4LSk?t=65)

    ![MutableLiveData Example](
    ![MutableLiveData Example](https://user-images.githubusercontent.com/117582/50354468-815ceb00-0500-11e9-82ae-571e17ce7ebe.png)
  8. humblehacker created this gist Dec 21, 2018.
    5 changes: 5 additions & 0 deletions mutablelivedata.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,5 @@
    # Don't expose `MutableLiveData`

    Example from [Android Jetpack: LiveData](https://youtu.be/OMcDk2_4LSk?t=65)

    ![MutableLiveData Example](