Skip to content

Instantly share code, notes, and snippets.

@ahmedeltaher
Last active November 6, 2021 18:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ahmedeltaher/63812865b0c31a6990ac5587ccf6d186 to your computer and use it in GitHub Desktop.
Save ahmedeltaher/63812865b0c31a6990ac5587ccf6d186 to your computer and use it in GitHub Desktop.

ROOM:

Room Benifits:

  • Object Mapping
  • Annotations to generate boilerplate codes.
  • Compile Time Error Checking.
  • Observable Queries include RXjava, flowables and livedata.
  • List, optional, Guava support.
  • Migration support between schemas.
  • Testing support.
  • by the end it is Google, means mainted by google it self, regualr upates, and bug fixes.

WorkManager:

Workmanager is a feature-rich API that compatibly schedules deferrable background work.

WorkManager Benifits:

  • Asynchronous one-off and periodic tasks.
  • Chaining with input/output. (some tasks can be input for other tasks)
  • Constrains (run the job when the constraints are valid, such as mobile has a network, batter low, or battery is not low, no enough storage space).
  • Handle Compatibility issues.
  • Best practice for system Health.
  • Guaranteed execution.
  • Query state to display in UI. (Restore the UI state of UI if the device restart)

ViewModel:

ViewModel is an object that provides data to UI components and survives the configuration change. such (screen rotation). it is lifecycle aware


LiveData:

Live date is an observable data holder. it is lifecycle aware.


LifeCycyle Benifits:

  • Avoid LifeCycle related UI loss: ViewModel.
  • Observability for your UI: LiveData
  • Avoid LifeCycle related memory leaks.
  • LiveData transformations.
  • UI-data base observation: livedata/Room
  • xml-ViewModel Observation : DataBinding.
  • Querying and observe UI lifecycle state.

Paging Benefits:

  • Flexible, supports any data sources.
  • Works out of the books with Room and Recycleview.
  • Support large and infinite length of lists.
  • Loaded data shown automatically with livedata.
  • support RXjava.

Navigation Benefits:

  • Handles Fragments transactions.
  • Implements proper up and back.
  • DeepLinking Support.
  • Easy animated transitions.
  • Common navigation pattern support.
Core Elements in Paging library:
  • PagedList.
  • Data source.
PagedList:
  • collection.
  • loads data in pages.
  • Asynchronous data loading.
DataSource:
  • BaseClass for loading snapshoot of data, we create data source by DataSourceFactory Object.
  • can be backed by:
    • Network.
    • Database.
    • File.
    • Any Other source.
PageList Adapter:
  • PagedList Adapter uses DiffUtil to update the data.
PageList :

we can configure pageList configuration, page size, load size hint, prefetch distance, setenablePlaceholder

ViewModel Best Pratice:

  • Don't reference your UI inside the viewModel. (we shouldn't see Context, Activity, Fragment...ect instances in ViewModel class) instead of that, we can use AndroidViewModel if we need to use context.

  • Use Viewmodel + onSaveInstanceState(), view model doesn't survive the shutdown process, onSaveInstanceState can survive the shutdown process, Viewmodel can hold a lot of data, onSaveInstanceState can't hold a lot of data, onSaveInstanceState require serialization. Store all your UI data in ViewModel, and store small as possible data in onSaveInstanceState to retain the UI state and survive the shutdown process. onSaveInstanceState -> (Data to reload Activity in Emergency)

Use SavedStateViewModel + onSaveInstanceState

ViewModel Saved instance state Persistent storage
Storage location in memory serialized to disk on disk or network
Survives configuration change Yes Yes Yes
Survives system-initiated process death No Yes Yes
Survives user complete activity dismissal/onFinish() No No Yes
Data limitations complex objects are fine, but space is limited by available memory only for primitive types and simple, small objects such as String only limited by disk space or cost / time of retrieval from the network resource
Read/write time quick (memory access only) slow (requires serialization/deserialization and disk access) slow (requires disk access or network transaction)
WorkManager:

WorkManager provides a unified solution for background work, taking android power saver features and the user API into account. It is part of Android Jetpack, it is backward compatible with Android API 14. Run with or without Google Play Services. Workmanager is used to run deferrable and grunted tasks.

1- Backing up images to the server. 2- Data analytics which not necessary to be run in the app working time. Background work, doesn't mean != background threading. WorkManager is not a replacement for:

  • Executor + thread pool
  • Kotlin Coroutines.
  • Rxjava. Workmanger works as conjunction with them, not a replacement for them.

WorkManager is not designed to trigger work in

  • Exact time. XX----> AlermManager
  • Immediate users expect work. XX -------> Foreground service. WorkManager with constrains, Constrains Builder (Network, connected... disconnected, Battery low...full)

More Work with workmanager:

  • periodic work.
  • unique work.
  • Tagged work.
  • Back off policies.

Why my work requests not running?

  • unsatisfied constrains.
  • overall system or app workload.(Android OS allow only a certin number of active jobs, workmanager is limited by the threadpool you give it in its configuration).
  • failed or incomplete prerequisites
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment