Building a simple notes app with new Architecture Components(Room+ViewModel+LiveData+Dagger) Part 1
Understanding the new architecture components…
Before starting with the code lets understand what these new components are why do they exists and how much they will benefits you as a developer in your app and the cost that you have to pay if you wish to migrate to using these components.
So why do we have these shiny new components ?
To understand this lets build a app to store names in a database and display them as a list in a fragment.
Lets start with building the database, to build a database we will require these classes :
- Table and Database Definition(Contract class).
And anyone who has written these classes they clearly know these are giant boilerplate and writing and maintaining them is a headache, especially the ContentProvider.
Next, our app retrieves the data from database and displays it using RecyclerView and also updates the list on any change made in database in real-time for this we need to implement build a listener that listen to changes in database and updates the ui automatically we can achieve this by using :
- Observer pattern.
Here the fragment is an observer and the CursorLoader is an observable that will pass new cursor to the fragment on every database change.
So whats the problem here is that the CursorLoader listen to changes on a particular uri and managing changes for different uri would require a lot of instances of the loader.
And finally database operation are expensive and the simple act of rotating your phone destroys the activity and all the data associated with it and then queries the database again just to get the same data that you already had there, nothing within database changed while rotating so why should the database be queried again ? why can’t we just use the same data again ?
Well these are the few common issues that developer at google try to address with these new components, they are meant to help developer write less boilerplate code, increase application robustness and make the app easily scale-able and modular.
The new architecture components basically consists of these components :
- Room Persistence Library.
- Paging Library.
- Life cycle aware components.
Lets look at each of them and try to understand each one of them :
Room Persistence Library
Room is an wrapper over sqlite and it is meant to address common issue with sqlite like it has compile time sqlstatement verification instead of runtime preventing those annoying crashes and it make it lot easier to convert sql queries to java objects.
There are 3 components that make up the room library, they are :
- Entity : A traditional java beans where each field is an column in database unless annotated with @Ignore. This class should be annotated with @Entity.
- DAO : Database Access Object, its an interface that defines the method representing the crud operations on database, this interfaces should be annotated with @DAO.
- Database : An abstract class that should extend the RoomDatabase and be annotated with @Database, its the main access point for the database.
It is an observable dataholder, it can store data of any type and allow you to monitor changes on it.The best part about livedata is that its lifecycle aware i.e. livedata only notifies its observer like activity/fragment if they are in active state this ensure :
- No Memory leaks.
- No manual handling of lifecycle events.
- Less crashes.
- Ensures the ui and underlying data stay in sync.
ViewModel is meant to bring a separation of ui and data layer, it is meant to act as an bridge between the ui and the data layer. The best part about viewmodel is that it survives configuration changes so rotating the device wont destroy the data associated with the activity and as long as the activity is not completely destroyed the viewmodel tied to it lives. You can also tie several activity/fragments to same viewmodel allowing seamless communication without those components knowing each exists.
This library is helps in loading data gradually as needed, which prevents unnecessary database/network calls and prevents device from overloading with data that it doesn't need.
We wont be using paging library as our app doesn't need it.
LifeCycle aware components
The new components also provide a package which lets you build lifecycle aware components allowing components to change their state based on the current lifecycle of the activity/fragment, thereby preventing memory leak and app crashes.
We wont be building any life cycle aware component in your app but you can easily do by implementing LifecycleObserver interface and handling action based on lifecycle.
In part 2 we will start building the app
The app uses Dagger to tie all the architecture components together and also Rx-Java is used to perform a sync operations on database.