5. The quick & dirty and wrong approach
Activity Adapter
Thread - worker
Rest Method Processor
Memory
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. 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. 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
11. 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.
12. Service
Activity
Data Processor Storage
Rest Method
Service
13. Activity ↔ Service interface
Starting the service:
Activity.startService(Intent I).
Initiating transactions and getting results:
Binder interface.
Intent interface.
20. Service
Activity
Data Processor Storage
Rest Method
Service
21. 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.
22. Data Processor
Before Delete;
Set status to “DELETING”
After Delete:
Delete DB row.
Before GET
After GET
Update DB
23. Service
Activity
Wrapper
Data Processor Storage
Rest Method
Service
24. 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.
25. 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.
39. Summary
Don't implement REST method in Activity / UI
layer.
Long running ops should run inside a service
And don't forget worker thread.
Persist early, persist often
Don't download what you already know.
Don't make me go crazy.
Sync adapter for non critical BG operations.