Skip to content

lemonboston/android-data-access-arch

Repository files navigation

Networking / Data Access architecture demo app using Retrofit-RxJava-EventBus

This is an Android sample app for a possible approach to the Networking and Data Access architecture illustrated with concrete use-case implementations.

The libraries used:
Retrofit with RxJava for handling the REST API calls.
EventBus to deliver the results and updates back from the data access layer to the presentation layer.
Rx features to handle errors, chained or parallel calls, fallback to offline store, and more.

Why them?
  • Retrofit
    Provides a simple, declarative way to create the REST API client as a Java interface, while it is also highly configurable through the underlying OkHttp client.
    And it supports RxJava return values.
  • RxJava
    Makes it easier to combine calls or execute custom processing chains without keeping states.
    Makes threading simple and declarative.
  • EventBus
    I think that for data access, the decoupling of the publishers and subscribers by using an event bus as middleman has benefits and can be a better approach over using the more direct connection with callbacks and the observer pattern.
    It gives the flexibility to send any kind of result, error, or progress update from any part of the processing chain and let the interested UI controller(s) listen to them independently from the initiator and from each other.
    It allows us to separate the data access layer more easily without the need and difficulties of keeping the direct callback references to the presentation layer.
    EventBus in particular has some useful features (threading, sticky events) over using for example the built-in BroadcastReceiver.

List of use-cases implemented

  • Basic use-case
    • Single network call executed on background thread, deliver results to UI on main thread
    • Handle screen rotation (configuration change): deliver result on rotated screen without restarting the call
    • Display loading indicator during the call
    • Can deliver result to different screen (user navigates to a different screen during the call)
  • Error handling
    • Error message displayed if something went wrong. Different messages for different errors.
    • Implementation:
      Errors propagate through the Rx chain and interpreted at single place at the end.
      Error event is sent to presentation layer where message is decided.
  • Ongoing call handling
    • There is only one call running at a time, initiating the call again is ignored.
    • Implementation:
      Rx Subscription reference is kept and checked if subscribed.
  • Offline storage
    • When online, network call is executed and result is saved and displayed
    • When offline, data is loaded from offline store
    • Implementation:
      Using Rx operators doOnSuccess and onErrorResumeNext are used to construct the appropriate chain.
  • Retry
    • Retries the call if it fails
    • Implementation:
      Using Rx operator RetryWhen
  • Cancelling
    • The ongoing network call can be cancelled
    • Implementation:
      Retrofit supports cancelling the call down to the network layer through the returned Observable/Single's unsubscribe() method.
  • Double load
    • Show offline data first if available but load from server too and refresh the displayed value.
    • Execute the operations in parallel.
    • Implementation:
      By combining the two Rx Observables with operator concatEager.
  • Parallel and Chained calls
    • User initiates a complex data access, receives the end result on the UI.
    • Requires multiple network calls. Both parallel and sequential executions might be needed.
    • Shows dialog about the progress steps.
    • Implementation:
      Rx operator zip for parallel execution and flatMap for sequential execution.
      Progress update events can be sent to the presentation layer at any point in Rx chain.
  • Background sync
    • Scheduled background data sync with server. Even when the app is not running or used by the user.
    • Implementation:
      (Scheduling is implemented simply with AlarmManager in this PoC but JobScheduler or GcmNetworkManager is a better solution in a real app).
  • HTTP Cache
    • Cache the network responses according to their Cache-Control headers
    • Implementation:
      Retrofit backed by OkHttp can take care of the caching, just has to be configured.
      (Can be switched on in Config class.)

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages