Playboy magazine was all the rage when I was in sixth grade. Penthouse was making inroads, too, as men placed their fascination with women on the glossy pages of magazines and sold them behind…
When I first used LiveData, I was just amazed by it.
Now I can get a callback whenever anyone changes the LiveData.
Also, the callback will only be received only if the UI is in an active state.
This is extremely useful for an app that shows UI based on a data model.
Using LiveData, I can observe the data model and update the UI whenever its value changes.
So, basically…
All this was so interesting that I decided to make my very own LiveData.
In this article, I'll show you how I made my own LiveData, what was my approach, and what were my learnings.
Let's start with the data holder part first.
Here's a basic implementation of LiveData
that will hold/wrap our data.
Now, it's time to make our LiveData
observable.
According to this definition, our LiveData
is the subject.
So our LiveData
should maintain a list of observers and all these observers should be notified whenever the value of our LiveData
changes.
Here's the implementation of LiveData
that is observable.
As you can see, now our LiveData
contains an ArrayList
of the observers that are attached to it.
For example, if we make a LiveData of an integer like this…
Then our observer's lambda expression would be like this…
To add and remove these observers from the list we have two methods observe
and removeObserver
respectively. Both of these methods take the observer as an argument of function type (T?) -> Unit
.
The main magic is in the setValue
method.
Here, after setting the value we are looping through all the observers that are attached and invoking them with the updated value. This way all the observers will receive the updated value and they can handle the new value appropriately.
Now, our LiveData
is ready to be observed.
This is how we can use it in our apps…
But! This version of LiveData has some problems.
Basically, our LiveData is not lifecycle aware.
Now, it’s time to make our LiveData
lifecycle aware.
So by passing the LifecycleOwner
to our LiveData, we can easily know and observe the lifecycle of the observer.
Here’s the implementation of LiveData
which accepts LifecycleOwner
and only notifies the observers only if they are in an active state.
As you can see, now our LiveData
contains a HashMap
of observers and owners associated with them.
Also when looping through all the observers, we are checking its owner's current state and invoking them only if they are in an active state.
Now, our observers will receive the updated value only if they are in an active state.
But! What about removing observers from the LiveData when their UI components are destroyed? And what about passing the current value to the observers immediately after they are attached to the LiveData?
For that, we need to monitor the lifecycle associated with the observers.
Then this LifecycleObserver can be attached to a LifecycleOwner
like this…
Here’s the implementation of LiveData
which is lifecycle aware.
As you can see, now the observe
method is initializing and attaching the LiveDataLifecycleObserver
to the owner.
And, the LiveDataLifecycleObserver
is having 3 annotated methods.
onStarted
and onResumed
will notify the observer with the updated value whenever the UI component comes to an active state.
onDestroyed
will remove the observer from the LiveData once the UI component is destroyed.
Now, our LiveData is finally complete and ready to use!
Making my own LiveData was a fun and interesting exercise.
I hope this article helped you in understanding the internals of LiveData.
The full source code can be found here!
Thanks for reading! Share this article if you found it useful.
Please do Clap 👏 to show some love :)
Nobody is going to discover a magic pill that allows you to eat like a pig and stay in tip top shape. It ain’t gonna happen Sunshine so wake the fuck up, rub that sleep out of your eyes and start…
When filling in your online profile on LinkedIn, it is easy to exaggerate your skills and experiences. This could result in awkward situations when starting your new job, for both you and your…
The Transgender Persons (Protection of Rights) Bill 2017 is going to be submitted by the central government in the winter parliament session. This comes on the heels of debates within the parliament…