SLIDE 1
Reactive Programming Professor Larry Heimann Carnegie Mellon - - PowerPoint PPT Presentation
Reactive Programming Professor Larry Heimann Carnegie Mellon - - PowerPoint PPT Presentation
Reactive Programming Professor Larry Heimann Carnegie Mellon University Information Systems Program API requirements in this sprint Quick refresh: delegate pattern in iOS Five steps for setting up the delegate pattern between two objects,
SLIDE 2
SLIDE 3
Quick refresh: delegate pattern in iOS
Five steps for setting up the delegate pattern between two objects, where object B is the delegate for object A, and object A will send out the messages:
- 1. Define a delegate protocol for object A; consider protocol extensions as
appropriate.
- 2. Make object B conform to the delegate protocol. It should put the name of the
protocol in its class line and implement the methods from the protocol.
- 3. Give object A an optional delegate variable. (This variable should be weak.)
- 4. Tell object A that object B is now its delegate.
- 5. Make object A send messages to its delegate when something interesting
happens, such as the user pressing the Cancel or Done buttons, or when it needs a piece of information.
SLIDE 4
Simple Delegate example
https://github.com/profh/67442_SimpleDelegate
SLIDE 5
– Krunoslav Zaher, creator of RxSwift
“If you've ever used an asynchronous callback based API, you've probably dealt with handling the response data ad-hoc all across your codebase, and have most likely decided there was no way to unit test it all... But, let me tell you - there is a better way, and it's called Rx!”
SLIDE 6
Reactive programming defined
“Reactive programming is a programming paradigm
- riented around data flows and the propagation
- f change. This means that it should be possible to
express static or dynamic data flows with ease in the programming languages used, and that the underlying execution model will automatically propagate changes through the data flow.”
SLIDE 7
What is reactive programming?
- A responsive application is the
goal.
- A responsive application is both
scalable and resilient.
Responsiveness is impossible to achieve without both scalability and resilience.
- A message-driven architecture is
the foundation of scalable, resilient, and ultimately responsive systems.
SLIDE 8
At the core, the observer pattern
- What is the observer
pattern?
- Allows other objects to
- bserve events and get
notifications when state changes.
- Useful when state is regularly
changing and/or many other
- bjects need to know when
state has changed
SLIDE 9
Review: simple Ruby example of observers
SLIDE 10
Reactive Programming in Swift
- RxSwift can be found on
GitHub: https://github.com/
ReactiveX/RxSwift
- Materials from lectures drawn
heavily from RxSwift book by Pillet, et al.
- Strongly recommended for
students wanting to learn more about reactive programming in general and RxSwift in particular
- Same true for this week’s lab
SLIDE 11
Key to RxSwift: Observables
The Observable<T> class provides the foundation of Rx code: the ability to asynchronously produce a sequence of events that can “carry” an immutable snapshot of data T. In the simplest words, it allows classes to subscribe for values emitted by another class
- ver time.
At its heart, an observable is just a sequence
Observer1 Observer2 Observer3
{
SLIDE 12
Finite and infinite data streams
Finite data streams include:
- File downloads (file sent in parts as chunks of data)
- GET requests from an API endpoint
- Timers counting off increments of time
Infinite data streams include:
- Changing the device orientation
- Tapping buttons
SLIDE 13
Simple examples of observables
SLIDE 14
Time to look at some code
SLIDE 15
Observable traits
- Singles will emit either a .success(value) or .error
- event. .success(value) is actually a combination of the .next
and .completed events. This is useful for one-time processes that will either succeed and yield a value or fail, such as downloading data or loading it from disk.
- A Completable will only emit a .completed or .error event. It
doesn't emit any value. You could use a completable when you only care that an operation completed successfully or failed, such as a file write.
- And Maybe is a mashup of a Single and Completable. It can either emit
a .success(value), .completed, or .error. If you need to implement an operation that could either succeed or fail, and optionally return a value on success, then Maybe is your ticket.
SLIDE 16
More code…
SLIDE 17
Next key concept: Subjects
A Subject is an object that can be both an observable and an
- bserver. There are four types of subjects:
- PublishSubject — starts empty and only emits new elements to
subscribers.
- BehaviorSubject — starts with an initial value and replays it or the latest
element to new subscribers.
- ReplaySubject — initialized with a buffer size and will maintain a buffer of
elements up to that size and replay it to new subscribers.
- Variable — wraps a BehaviorSubject, preserves its current value as state,
and replays only the latest/initial value to new subscribers.
SLIDE 18
PublishSubjects
- The first subject emits three events and completes
- The second subject subscribes after first event, but gets the other two
- The third subject subscribes after the second event. First subject
notifies both observers of the last event
SLIDE 19
BehaviorSubjects
- The first subject emits three events and completes
- The second subject subscribes after first event, but gets the first event
immediately and notified of the other events when they occur
- The third subject subscribes after the second event. It gets the prior
event immediately (but not the original) and other events when they
- ccur
SLIDE 20
ReplaySubjects
- The first subject emits three events and completes and notifies the
second subject as they occur
- The third subject subscribes after the second event. It gets all the prior