KEMBAR78
Android asynchronous programming | PPTX
Asynchronous Programming
•Sync & Async
•Thread & Handler
•AsyncTask
•RxAndroid/RxJava
2
Sync & Async
3
Sync & Async
4
Sync & Async
5
Synchronous call Asynchronous call
● Caller WAITS for method to
complete
● Blocking
● Easy to program/understand
● Method returns immediately to
caller and executes callback
● Non-blocking
● Run several methods
concurrently
● Scalability
● Harder to program
Thread & Handlerhttps://developer.android.com/training/multiple-threads/communicate-ui.html
6
Thread
7
Thread
8
What is it?
• A basic unit of CPU utilization
• Lightweight process
• Multiple threads are executed within a process
Handler
9
Handler
10
What is it?
• Part of the Android system's framework
• Communicate between threads:
– Send message
– Process message
Handler
11
Thread & Handlerhttps://developer.android.com/training/multiple-threads/communicate-ui.html
12
AsyncTaskhttps://developer.android.com/reference/android/os/AsyncTask.html
What is it?
• Helper class around Thread and Handler
• Perform background operations
• Publish results on the UI thread
13
AsyncTaskhttps://developer.android.com/reference/android/os/AsyncTask.html
14
AsyncTaskhttps://developer.android.com/reference/android/os/AsyncTask.html
15
When to use? Trigger Triggered From Runs On Limitations /
Drawbacks
- Relatively long
task (UI thread
blocking) with a
need to
communicate with
main thread.[3]
- For tasks in
parallel use
multiple instances
OR Executor [1]
Call to method
execute()
Main Thread Worker thread.
However, Main
thread methods
may be invoked in
between to
publish progress.
- one instance can
only be executed
once (hence
cannot run in a
loop) [2]
- Must be created
and executed from
the Main thread
RxAndroidhttp://reactivex.io/
16
RxAndroidhttp://reactivex.io/
17
ReactiveX is:
• A combination of the best ideas from
– The Observer pattern
– The Iterator pattern
– Functional programming
• An API for asynchronous programming with observable streams.
RxAndroidhttp://reactivex.io/
18
RxAndroidhttps://nhancv.github.io/product/rxjava/
19
Observable
In ReactiveX an observer subscribes to an Observable.
• onNext
• onError
• onCompleted
RxAndroidhttps://nhancv.github.io/product/rxjava/
20
Observer
• An observer subscribes to an Observable.
• An Observable emits items or sends notifications to its observers by
calling the observers’ methods.
RxAndroidhttps://nhancv.github.io/product/rxjava/
21
PublishSubject
• Emits to an observer only those items that are emitted by the source
Observable(s) subsequent to the time of the subscription.
RxAndroidhttps://nhancv.github.io/product/rxjava/
22
Scheduler
• The SubscribeOn operator designates which thread the Observable will
begin operating on.
• The ObserveOn operator specifies a different Scheduler that the
Observable will use to send notifications to its observers.
Varieties of Scheduler
• Schedulers.computation( )
• Schedulers.io( )
• Schedulers.newThread( )
RxAndroidhttps://nhancv.github.io/product/rxjava/
23
RxAndroidhttps://nhancv.github.io/product/rxjava/
24
Operators
Create observable Transforming
• create
• defer
• just
• from
• map
• flat map
RxAndroidhttp://reactivex.io/
25
Operators
Filtering Combining
• debounce
• distinct
• filter
• merge
• zip
26
THANK YOU
for
YOUR ATTENTION
https://github.com/nhancv/nc-android-async
27

Android asynchronous programming