Reactive programming is a modern and trending technology which allows development of background task easy and managable. This presentation will specically explain use of Rx plug-in in android and ios applications
RxJava & RxSwift
http://reactivex.io/
3. Rx = ReactiveX = Reactive Extensions
➔ ReactiveX is a library for composing asynchronous and event-
based programs by using observable sequences.
➔ It extends the observer pattern to support sequences of data
and/or events and adds operators that allow you to compose
sequences together declaratively while abstracting away
concerns about things like low-level threading, synchronization,
thread-safety, concurrent data structures, and non-blocking I/O
4. The Difference:
Functional reactive programming operates on values that
change continuously over time, while ReactiveX operates
on discrete values that are emitted over time
Functional Programming
+
Reactive Programming
8. ➔ ANDROID (Rx-Java2)
➔ iOS (Rx-Swift3)
GETTING STARTED ...
compile 'io.reactivex.rxjava2:rxandroid:2.0.1'
compile 'io.reactivex.rxjava2:rxjava:2.0.1'
# Podfile
target 'YOUR_TARGET_NAME' do
pod 'RxSwift', '~> 2.0 / 3.0'
pod 'RxCocoa', '~> 2.0 / 3.0'
end
9. Before we start ...
WHAT ARE OPERATORS ?
➔ Items emitted by an Observable can be transformed, modified, and
filtered through Operators before notifying the subscribed Observer
object(s).
➔ Applying an Operator typically returns another Observable as a result,
which is convenient as this allows us to chain multiple operations to
obtain a desired result
➔ Eg. just(), from(), map(), filter(), combine() etc.
11. ➔ ANDROID
➔ iOS
1. Create an Observable
//Observable. This will emit single data
Observable<String> observable = Observable.just("Item 1");
//Observable. This will emit sequence of the data
Observable<String> observable = Observable.fromArray(new String[]{"1", "2", "3", "4"})
//Observable. This will emit single data
let observable = Observable.just("Item 1");
//Observable. This will emit sequence of the data
let observable = Observable.from(["1", "2", "3", "4"])
12. Observer is notified with three events when data is emitted by the observable
stream:
1. Observer #onNext(T)— invoked when an item is emitted from the stream
2. Observable #onError(Throwable)— invoked when an error has occurred within
the stream
3. Observable #onCompleted()— invoked when the stream is finished emitting
items.
2. Create an Observer & Schedule it
13. Example
Observer<String> observer = new
Observer<String>() {
@Override
public void onCompleted() {..}
@Override
public void onError(Throwable e) {..}
@Override
public void onNext(String s) {..}
};
observable.subscribe(observer);
1.let subscription = observable.subscribe {
event in
switch event {
case .next(let value):
print(value)
case .error(let error):
print(error)
case .completed:
print("completed")
}
}
2.let sub = observable.subscribe
(onNext:{ print($0) })
➔ Android ➔ iOS
14.
15. Do you want to play with more
operators?
Marble Diagram
16. Example 1 : map()
➔ Android ➔ iOS
Observable<Integer> observable =
Observable.fromArray(1, 2, 3, 4)
observable..map(new
Function<Integer, Integer>() {
public Integer apply(Integer num){
return num * num;
}
}).subscribe(observer);
let observable =
Observable.from([1, 2, 3, 4])
let sub =
observable.map {$0 * $0}
.subscribe
(onNext:{
print($0)
})
OUTPUT: 1 4 9 16
17. Example 2 : filter()
➔ Android ➔ iOS
Observable<Integer> observable =
Observable.fromArray(20, 10, 5, 40)
observable.filter(new
Predicate<Integer>() {
public boolean test(Integer num) {
return num > 10;
}
}).subscribe(observer);
let observable =
Observable.from([20, 10, 5, 40])
let sub =
observable.filter {$0 > 10}
.subscribe
(onNext:{
print($0)
})
OUTPUT: 30 22 60
19. Subjects
“A Subject is a sort of bridge or proxy that acts both as an
observer and as an Observable. Because it is an observer, it
can subscribe to one or more Observables, and because it is
an Observable, it can pass through the items it observes, and
reemit them or emit new items.”
➔ PublishSubject
Emits to an observer only those items that are emitted by the source
Observable(s)
➔ ReplaySubject
Emits to any observer all of the items that were emitted by the source
Observable(s)
21. Example 5 : zip()
➔ Android
PublishSubject<String> s1 =
PublishSubject.create();
PublishSubject<Integer> s2 =
PublishSubject.create();
ObservablePublish.zip(s1, s2, new
BiFunction<String, Integer, String>() {
public String apply(Integer a, String b) {
return a+"-"+b;
}
}).subscribe(observer);
s1.onNext(“City 1”);
s1.onNext(“City 2”);
s2.onNext(22);
s2.onNext(25);
➔ iOS
let s1 = PublishSubject<String>()
let s2 = PublishSubject<Int>()
Observable<(String,Int)>.zip(s1, s2)
{ (a, b) throws -> (String,Int) in
return (a,b)
}.subscribe {
print("($0.element!.0) -($0.element!.1)")
}
s1.onNext(“City 1”);
s1.onNext(“City 2”);
s2.onNext(22);
s2.onNext(25);
OUTPUT : City 1 - 22, City 2 - 25
22. In Rx, concurrency is managed by the Scheduler, when you want to perform
multithreading with multiple streams and also operate on UI thread
simultaneously.
Main example of threads are
1. Computational Thread
2. I/O Thread
3. UI Thread
Scheduler
➔ subscribeOn() : By using this method you can define on which thread the
observable should run.
➔ observeOn() : By using this method you can define on which thread the
observer should run.
23. Manage Subscription
CompositeDisposable mSubscription = new CompositeDisposable();
...
mSubscription.add(disposable1); //Add disposable 1
mSubscription.add(disposable2); //Add disposable 2
...
public void onDestroy() {
//Unsubscribe both subscriptions.
mSubscription.dispose();
}
//disposable1.dispose(); also works
➔ Composite Subscriptions
➔ Unsubscribe Subscriptions