Skip to content

Instantly share code, notes, and snippets.

@hgayan7
Last active June 14, 2019 07:09
Show Gist options
  • Save hgayan7/16d0eb9c750eee1194d91a16ffcb03b8 to your computer and use it in GitHub Desktop.
Save hgayan7/16d0eb9c750eee1194d91a16ffcb03b8 to your computer and use it in GitHub Desktop.
Android Notes
LiveData features -
1.Synchronous
2.Used for notifying
3.No memory leaks
4.No crashes
5.No manual lifecycle handling
6.It acts as a data holder.
Implementation steps -
1.Create LiveData instance to hold certain type of data.Usually done within ViewModel class.
2.Create Observer object that defines onChanged(), which controls what will happen when LiveData object held data changes.
Usually Observer object is created in Activity/Fragment.
3.Attach Observer object to LiveData object using observe() method. observe() takes a LifecycleOwner object.This
subscribes the Observer object to the LiveData object so that it will be notified of changes.
MVVM architecture -
1.View
- Activity/Fragment : Configures the view, observes and exhibits LiveData elements taken from ViewModel.
2.ViewModel -
- Most of the logic is written here.
- It never references the views directly and the updates on the data are always done by LiveData entity.
- It is lifecycle aware.
3.Model
- Responsible for fetching data from all available sources i.e webservice or database.
Room - Provides abstraction layer over SQLite to allow fluent database access.
Elements -
1.Database
- Conatains the database holder and serves as the main access point for underlying connection to
the relational DB.
- Annotated with @Database
- Abstract class extending RoomDatabase
- Includes list of Enitities
- Contains an abstract method that returns the class that is annotated with @Dao
- Instance of DB can be acquired by
- Calling Room.databaseBuilder()
- Calling Room.inMemoryDatabaseBuilder()
2.Entity
- Represnts table within Database
- Annotated with @Entity
3.DAO
- Contains method used for accessing the Database.(CRUD operations)
- Annotated with @Dao
Features of RxJava -
1.Asynchronous programming
2.In reactive programming the consumer reacts to data as it comes in.
3.RxJava and LiveData compliment each other if used together i.e do everything with RxJava and at the end change the
Observable into LiveData and update the UI.(As Views observes to the LiveData in ViewModel)
4.Schedulers - Components in Rx which tells Observer and Observable on which thread they should run.
5.It can be used to do any background process like accessing database,network calls etc.
Main tasks in Reactive programming -
- source of data (Observable)
- consumer of data (Observer)
- connection of consumer to source
Entities of RxJava -
1.Flowable<T>
- Emits zero or n items and terminates with a success or error event.
- Can control how fast source emits items
2.Observable<T>
- Emits zero or n items and terminates with a success or error event.
3.Single<T>
- Emits either a single item or and error event.
- It is the reactive version of method call.
4.Maybe<T>
- Succeeds with an item, no item or an error event.
- It is the reactive version of an Optional.
5.Completable -
- Either completes with a success or error event.
- Never emits items.
- It is the reactive version of Runnable.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment