Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.
Android REST Client Architecture
About meIndependent Android Consultant and TrainerCo-founder of IAAGDG & GDE+Ran Nachmany
Rest Architecture Client and server Client send request get or change resource. Twitter, facebook, Google, flight schedule...
The quick & dirty approach              Activity               Adapter   Thread - worker    Rest Method          Processor...
The quick & dirty and wrong approach              Activity               Adapter   Thread - worker    Rest Method         ...
Android Laws Foreground Apps.   must have the resources they need Visible activity or running service.   Should keep runni...
Implications: Android may (and will) shut down your process. Background apps should be ready to die   Handle onPause() and...
The quick & dirty and wrong approach Killed during transaction   Request may or may not executed on server side. No persis...
Solution #1: Service API
Service Service is designed to run in the background. Has no user interface. Runs on main thread (i.e. UI thread)   You st...
Service             Activity          Data Processor   Storage           Rest Method            Service
Activity ↔ Service interface Starting the service:   Activity.startService(Intent I). Initiating transactions and getting ...
Activity ↔ Service interface Start Service
Binder Interface - Activity
Binder Interface - Activity
Binder Interface – Service side
Intent Interface – Activity side
Intent Interface – Service side
Service             Activity          Data Processor   Storage           Rest Method            Service
Data Processor Holds a “mirror” of the data in the server. Runs before and after each transaction. Before Post / Put:   Pr...
Data Processor Before Delete;   Set status to “DELETING” After Delete:   Delete DB row. Before GET After GET   Update DB
Service             Activity            Wrapper          Data Processor   Storage           Rest Method            Service
Service Wrapper Singletone. Exposes simple async API to be used by UI. Upon service request:   Check if method is already ...
Handling service callback - Activity Activity has its own lifecycle.   Activity is still active when the response arrives....
Solution #2: Content Provider
Content Provider Mechanism to share /transfer data across boundries. Usually used to share data between apps.
Activity & Cursor Adapter           Content Provider  Wrapper ServiceData Processor Rest Method
Activity & Cursor Adapter1. Query (insert /update/ delete)                                Content Provider                ...
Activity & Cursor Adapter1. Query (insert /update/ delete)                                Content Provider2. Start (intent...
Activity & Cursor Adapter1. Query (insert /update/ delete)                                Content Provider2. Start (intent...
Activity & Cursor Adapter1. Query (insert /update/ delete)                                Content Provider2. Start (intent...
Activity & Cursor Adapter1. Query (insert /update/ delete)                                Content Provider2. Start (intent...
Activity & Cursor Adapter1. Query (insert /update/ delete)                                Content Provider2. Start (intent...
Activity & Cursor Adapter1. Query (insert /              7. Notify Contentupdate/ delete)                 Observer        ...
Activity & Cursor Adapter1. Query (insert /              7. Notify Content       8. Queryupdate/ delete)                 O...
Solution #3: Content Provider + SyncAdapter
Activity & Cursor Adapter         Content ProviderSync AdapterData Processor Rest Method
Summary Dont implement REST method in Activity / UI layer. Long running ops should run inside a service   And dont forget ...
Thank You
Androd rest client architecture
Upcoming SlideShare
Loading in …5
×

Androd rest client architecture

7,141 views

Published on

In this presentation I go over the challenges in building Android REST client and offer several solutions to mitigate them.

Published in: Technology

Androd rest client architecture

  1. 1. Android REST Client Architecture
  2. 2. About meIndependent Android Consultant and TrainerCo-founder of IAAGDG & GDE+Ran Nachmany
  3. 3. Rest Architecture Client and server Client send request get or change resource. Twitter, facebook, Google, flight schedule etc.
  4. 4. The quick & dirty approach Activity Adapter Thread - worker Rest Method Processor Memory
  5. 5. The quick & dirty and wrong approach Activity Adapter Thread - worker Rest Method Processor Memory
  6. 6. Android Laws Foreground Apps. must have the resources they need Visible activity or running service. Should keep running. As long as it does not break #1 Background Apps kept in memory to reduce start up and latency time. As long as it does not break #1 or #2.
  7. 7. Implications: Android may (and will) shut down your process. Background apps should be ready to die Handle onPause() and onStop(). Background apps should be “thin”. Being good android citizen increases your chances to live longer.
  8. 8. The quick & dirty and wrong approach Killed during transaction Request may or may not executed on server side. No persistence data High latency. Waste of bandwidth. Waste of battery
  9. 9. Solution #1: Service API
  10. 10. Service Service is designed to run in the background. Has no user interface. Runs on main thread (i.e. UI thread) You still need to create a worker thread.
  11. 11. Service Activity Data Processor Storage Rest Method Service
  12. 12. Activity ↔ Service interface Starting the service: Activity.startService(Intent I). Initiating transactions and getting results: Binder interface. Intent interface.
  13. 13. Activity ↔ Service interface Start Service
  14. 14. Binder Interface - Activity
  15. 15. Binder Interface - Activity
  16. 16. Binder Interface – Service side
  17. 17. Intent Interface – Activity side
  18. 18. Intent Interface – Service side
  19. 19. Service Activity Data Processor Storage Rest Method Service
  20. 20. Data Processor Holds a “mirror” of the data in the server. Runs before and after each transaction. Before Post / Put: Prepare transaction data Create DB row with status “updating” After Post / Put: Clear the “updating” status.
  21. 21. Data Processor Before Delete; Set status to “DELETING” After Delete: Delete DB row. Before GET After GET Update DB
  22. 22. Service Activity Wrapper Data Processor Storage Rest Method Service
  23. 23. Service Wrapper Singletone. Exposes simple async API to be used by UI. Upon service request: Check if method is already pending. Create intent. Generate req id. Hold binder. Return Req id. Handle callback from the service.
  24. 24. Handling service callback - Activity Activity has its own lifecycle. Activity is still active when the response arrives. Activity is paused, resumed and then the response arrives. Activity is paused with the response arrives, and than resumed. Adapter.notifyDataSetChanged() when needed.
  25. 25. Solution #2: Content Provider
  26. 26. Content Provider Mechanism to share /transfer data across boundries. Usually used to share data between apps.
  27. 27. Activity & Cursor Adapter Content Provider Wrapper ServiceData Processor Rest Method
  28. 28. Activity & Cursor Adapter1. Query (insert /update/ delete) Content Provider Wrapper Service Data Processor Rest Method
  29. 29. Activity & Cursor Adapter1. Query (insert /update/ delete) Content Provider2. Start (intent) Wrapper Service Data Processor Rest Method
  30. 30. Activity & Cursor Adapter1. Query (insert /update/ delete) Content Provider2. Start (intent) Wrapper3. Start Service Service Data Processor Rest Method
  31. 31. Activity & Cursor Adapter1. Query (insert /update/ delete) Content Provider2. Start (intent) Wrapper3. Start Service Service Data Processor4. Execute method Rest Method
  32. 32. Activity & Cursor Adapter1. Query (insert /update/ delete) Content Provider2. Start (intent) Wrapper3. Start Service Service Data Processor4. Execute method 5. Process respons Rest Method
  33. 33. Activity & Cursor Adapter1. Query (insert /update/ delete) Content Provider2. Start (intent) Wrapper3. Start Service 6. Insert/ update/ Service delete Data Processor4. Execute method 5. Process respons Rest Method
  34. 34. Activity & Cursor Adapter1. Query (insert / 7. Notify Contentupdate/ delete) Observer Content Provider2. Start (intent) Wrapper3. Start Service 6. Insert/ update/ Service delete Data Processor4. Execute method 5. Process respons Rest Method
  35. 35. Activity & Cursor Adapter1. Query (insert / 7. Notify Content 8. Queryupdate/ delete) Observer Content Provider2. Start (intent) Wrapper3. Start Service 6. Insert/ update/ Service delete Data Processor4. Execute method 5. Process respons Rest Method
  36. 36. Solution #3: Content Provider + SyncAdapter
  37. 37. Activity & Cursor Adapter Content ProviderSync AdapterData Processor Rest Method
  38. 38. Summary Dont implement REST method in Activity / UI layer. Long running ops should run inside a service And dont forget worker thread. Persist early, persist often Dont download what you already know. Dont make me go crazy. Sync adapter for non critical BG operations.
  39. 39. Thank You

×