Introduction to reactive programming
- 2. CONTENTS
Introduction to Reactive programming
Imperative Programming
Reactive Programming
Why should you care
Four Reactive Principles
RxExtensions
Marble Diagrams and common operators
Observable
Observer
Subscriber
Questions
- 3. What is Reactive Programming?
3
Imperative programming
programming paradigm that uses statements that change a program's state
Example: Buying newspaper in a shop
var a = 5
var b = 6
var sum = a + b
a = 7
// sum is still 11
Reactive Programming:
programming paradigm oriented around data flows and the propagation of change
Examples:
Spreadsheets
User Interface Menus
Example: Subscribing to newspapers
- 4. WHY SHOULD YOU CARE
• Easy to read/reason and understand
• Cross platform/language concept
• Less error prone
• Open source
• To avoid Spaghetti / Boilerplate code
• A lot of very popular library have rx wrappers
• Eg: RxAlamofire for networking in swift
- 6. • Fast
• Should provide feedback to user input
• Notify that an error has occurred
RESPONSIVE
RESILIENT
• Ability to recover from failure/exceptions
• Eg: retrying network calls
- 7. • Scalable
• Able to handle growing amount of work
ELASTIC
MESSAGE DRIVEN
• Reacts to events that happen not blocked waiting for them
• Data (events) flow throughout the system
- 8. RX EXTENSIONS
RX = Reactive Extensions
Reactive Extensions (Rx) is a library for composing asynchronous and
event-based programs using observable sequences
RX = Iterator Pattern + Observer Pattern + Schedulers
TECHNICALLY
- 9. • Provide a way to access the elements of an collection
sequentially without exposing its underlying representation
• For any collection we get the iterator from which each
element can be traversed / accessed
• Pull based mechanism i.e. consumer is responsible for
creating the iterator to access each element at a time from
the producer (collection)
ITERATOR PATTERN
- 10. • The observer pattern is a software design pattern in which an
object, called the subject, maintains a list of its dependents,
called observers, and notifies them automatically of any state
changes, usually by calling one of their methods. It is mainly
used to implement distributed event handling systems.
• The producer is responsible for providing the consumer with
data push mechanism
OBSERVABLE PATTERN
- 11. SCHEDULERS
• By default observable sequences will observe events and react to
them in the same thread. Schedulers are a way to change this
behavior as a way to distribute tasks across threads.
• Thread abstractions for concurrent and asynchronous work
• Especially useful in mobile devices where there is a dedicated
thread called Main/UI thread for handling ui changes
• Specifies in which thread the computations will occur e.g. io,
computation, Main
- 14. OBSERVABLE
// skeleton
public class Observable<T> {
// many rx operators
Subscription subscribe(final Observer<T> observer) {}
}
• Data source i.e. creates events
• Provides data to observer using onNext() function
• Notify that an error has occurred using onError() function
• Notify completion of stream/data using onComplete()
function
- 15. OBSERVER
public interface Observer<T> {
void onCompleted();
void onError(Throwable e);
void onNext(T t);
}
• Observer subscribes to an Observable
• Get notified/react to next, error and completion events
• Primary reason why threads are not blocked in reactive programming
• onNext(): An Observable calls this method whenever the Observable emits
an item. This method takes as a parameter the item emitted by the
Observable.
• onError(): An Observable calls this method to indicate that it has failed to
generate the expected data or has encountered some other error. It will
not make further calls to onNext or onCompleted. The onError method
takes as its parameter an indication of what caused the error.
• onCompleted(): An Observable calls this method after it has called onNext
for the final time, if it has not encountered any errors.
- 16. SUBSCRIBERS
public interface Subscription {
void unsubscribe();
boolean isUnsubscribed();
}
class Subscriber<T> implements Observer<T>, Subscription {
}
• A Subscriber is an Observer that can also unsubscribe from
that data source (through the Subscription interface).