Androd rest client architecture

5,446 views
5,127 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
0 Comments
13 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
5,446
On SlideShare
0
From Embeds
0
Number of Embeds
8
Actions
Shares
0
Downloads
1
Comments
0
Likes
13
Embeds 0
No embeds

No notes for slide

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

×