Giter Club home page Giter Club logo

android-data-access-arch's Introduction

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.)

android-data-access-arch's People

Stargazers

Bhargav Pandya avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.