Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.
SWIFT & REACTIVEX
Asynchronous Event-Based Funsies with RxSwift
WHO I AM
➤ Aaron Douglas
➤ Milwaukee, WI USA
➤ Mobile Maker for Automattic
(WordPress.com)
➤ Remote full time 3+ years
➤ @...
REACTIVE?

FUNCTIONAL?

MEH?
FUNCTIONAL & REACTIVE PROGRAMMING
➤ Reactive Programming
➤ Asynchronous Data Flow
➤ Functional Programming
➤ Map, Reduce, ...
WHAT IS RXSWIFT?
WHAT IS RXSWIFT?
➤ Based on ReactiveX
➤ Many different ports: Java, JavaScript, .NET, Scala, Clojure, Swift, Kotlin, PHP, …...
OBSERVER PATTERN
NSNOTIFICATIONCENTER
NSNotificationCenter
.defaultCenter()
.addObserver(self,
selector:"downloadImage:",
name: "BLDownload...
OBSERVER PATTERN
9
ITERATOR PATTERN
ITERATOR PATTERN
GENERATORS, SEQUENCES,
OH MY!
GENERATORS
public protocol GeneratorType {
associatedtype Element
public mutating func next() -> Self.Element?
}
GENERATORS
class CountdownGenerator: GeneratorType {
typealias Element = Int
var element: Element
init<T>(array: [T]) {
se...
SEQUENCES
public protocol SequenceType {
associatedtype Generator : GeneratorType
public func generate() -> Self.Generator...
SEQUENCES
class ReverseSequence<T>: SequenceType {
var array: [T]
init(array: [T]) {
self.array = array
}
func generate() ...
OBSERVABLES
OBSERVERTYPE
public protocol ObserverType {
associatedtype E
func on(event: Event<E>)
}
public enum Event<Element> {
case ...
OBSERVABLETYPE
public protocol ObservableType : ObservableConvertibleType {
associatedtype E
func subscribe<O: ObserverTyp...
VISUALIZATIONS OF SEQUENCES
--1--2--3--4--5--6--| // terminates normally
--a--b--a--a--a---d---X // terminates with error
...
MAKING AN OBSERVABLE
let disposeBag = DisposeBag()
Observable.just("X")
.subscribe { event in
print(event)
}
.addDisposabl...
MAKING AN OBSERVABLE
let disposeBag = DisposeBag()
Observable.of("W", "X", "Y", "X")
.subscribeNext { element in
print(ele...
MAKING AN OBSERVABLE
let disposeBag = DisposeBag()
["W", "X", "Y", "Z"].toObservable()
.subscribeNext { print($0) }
.addDi...
SUBSCRIBING
someObservable.subscribe(
onNext: { print("Element: ", $0) },
onError: { print("Error: ", $0) },
onCompleted: ...
OPERATORS
MAP
MAP
let disposeBag = DisposeBag()
Observable.of(1, 2, 3)
.map { $0 * 10 }
.subscribeNext { print($0) }
.addDisposableTo(di...
FILTER
FILTER
let disposeBag = DisposeBag()
Observable.of(2, 30, 22, 5, 60, 1)
.filter { $0 > 10 }
.subscribeNext { print($0) }
....
SCAN
SCAN
let disposeBag = DisposeBag()
Observable.of(1, 2, 3, 4, 5)
.scan(0) { aggregateValue, newValue in
aggregateValue + ne...
MERGE
MERGE
let disposeBag = DisposeBag()
let subject1 = PublishSubject<String>()
let subject2 = PublishSubject<String>()
Observ...
RXMARBLES.COM
DISPOSING
.dispose()
.addDisposableTo(disposeBag)
COCOA + RXSWIFT
BINDINGS
totCountStepper
.rx_value
.subscribeNext { value in
self.totalNumberOfTots.value = Int(value)
}
.addDisposableTo(...
BINDINGS
➤ NSTextStorage
➤ UIActivityIndicatorView
➤ UIAlertAction
➤ UIApplication
➤ UIBarButtonItem
➤ UIButton
➤ UICollec...
AN EXAMPLE
TATER TOT TIMER
RXSWIFT IN THE WILD
WHERE TO LEARN MORE
WHERE TO LEARN MORE
➤ ReactiveX RxSwift main repo
➤ https://github.com/ReactiveX/RxSwift/
➤ FRP iOS Learning Resources
➤ h...
AARON DOUGLAS@astralbodies
http://astralbodi.es
Swift & ReactiveX – Asynchronous Event-Based Funsies with RxSwift
Upcoming SlideShare
Loading in …5
×

Swift & ReactiveX – Asynchronous Event-Based Funsies with RxSwift

627 views

Published on

This is an introduction to reactive concepts using Swift specifically with ReactiveX’s implementation, RxSwift. ReactiveX is an API for asynchronous programming with observable streams originally implemented with .NET and LINQ. ReactiveX is a combination of the best ideas from the Observer pattern, the Iterator pattern, and functional programming.

You’ll learn about all the basic moving parts of RxSwift and why you want to use it in your application.

Original presented 23-Aug-2016 at 360iDev 2016 - Denver, CO.

Published in: Technology
  • Hello! I can recommend a site that has helped me. It's called ⇒ www.HelpWriting.net ⇐ So make sure to check it out!
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here

Swift & ReactiveX – Asynchronous Event-Based Funsies with RxSwift

  1. 1. SWIFT & REACTIVEX Asynchronous Event-Based Funsies with RxSwift
  2. 2. WHO I AM ➤ Aaron Douglas ➤ Milwaukee, WI USA ➤ Mobile Maker for Automattic (WordPress.com) ➤ Remote full time 3+ years ➤ @astralbodies
  3. 3. REACTIVE?
 FUNCTIONAL?
 MEH?
  4. 4. FUNCTIONAL & REACTIVE PROGRAMMING ➤ Reactive Programming ➤ Asynchronous Data Flow ➤ Functional Programming ➤ Map, Reduce, Filter ➤ Avoiding State ➤ Immutable Data ➤ Declarative Paradigm - logic of computation rather than describing flow ➤ Implementations - ReactiveCocoa/RAC, RxSwift
  5. 5. WHAT IS RXSWIFT?
  6. 6. WHAT IS RXSWIFT? ➤ Based on ReactiveX ➤ Many different ports: Java, JavaScript, .NET, Scala, Clojure, Swift, Kotlin, PHP, … ➤ Extends the Observer Pattern ➤ Related to Iterable Pattern ➤ Swift itself provides some protocols with equivalence ➤ SequenceType ➤ GeneratorType
  7. 7. OBSERVER PATTERN
  8. 8. NSNOTIFICATIONCENTER NSNotificationCenter .defaultCenter() .addObserver(self, selector:"downloadImage:", name: "BLDownloadImageNotification", object: nil) NSNotificationCenter .defaultCenter() .postNotificationName("BLDownloadImageNotification", object: self, userInfo: ["imageView": coverImage, "coverUrl": albumCover])
  9. 9. OBSERVER PATTERN 9
  10. 10. ITERATOR PATTERN
  11. 11. ITERATOR PATTERN
  12. 12. GENERATORS, SEQUENCES, OH MY!
  13. 13. GENERATORS public protocol GeneratorType { associatedtype Element public mutating func next() -> Self.Element? }
  14. 14. GENERATORS class CountdownGenerator: GeneratorType { typealias Element = Int var element: Element init<T>(array: [T]) { self.element = array.count } func next() -> Element? { guard element > 0 else { return nil } element -= 1 return element } } let xs = ["A", "B", "C"] let generator = CountdownGenerator(array: xs) while let i = generator.next() { print("Element (i) of the array is (xs[i])") } Element 2 of the array is C Element 1 of the array is B Element 0 of the array is A
  15. 15. SEQUENCES public protocol SequenceType { associatedtype Generator : GeneratorType public func generate() -> Self.Generator }
  16. 16. SEQUENCES class ReverseSequence<T>: SequenceType { var array: [T] init(array: [T]) { self.array = array } func generate() -> CountdownGenerator { return CountdownGenerator(array: array) } } let reverseSequence = ReverseSequence(array: xs) let reverseGenerator = reverseSequence.generate() while let i = reverseGenerator.next() { print("Index (i) is (xs[i])") } for i in ReverseSequence(array: xs) { print("Index (i) is (xs[i])") } Index 2 is C Index 1 is B Index 0 is A Index 2 is C Index 1 is B Index 0 is A
  17. 17. OBSERVABLES
  18. 18. OBSERVERTYPE public protocol ObserverType { associatedtype E func on(event: Event<E>) } public enum Event<Element> { case Next(Element) case Error(ErrorType) case Completed }
  19. 19. OBSERVABLETYPE public protocol ObservableType : ObservableConvertibleType { associatedtype E func subscribe<O: ObserverType where O.E == E>(observer: O) -> Disposable } public protocol ObservableConvertibleType { associatedtype E func asObservable() -> Observable<E> }
  20. 20. VISUALIZATIONS OF SEQUENCES --1--2--3--4--5--6--| // terminates normally --a--b--a--a--a---d---X // terminates with error ---tap-tap-------tap---> // infinite; never ends
  21. 21. MAKING AN OBSERVABLE let disposeBag = DisposeBag() Observable.just("X") .subscribe { event in print(event) } .addDisposableTo(disposeBag)
  22. 22. MAKING AN OBSERVABLE let disposeBag = DisposeBag() Observable.of("W", "X", "Y", "X") .subscribeNext { element in print(element) } .addDisposableTo(disposeBag)
  23. 23. MAKING AN OBSERVABLE let disposeBag = DisposeBag() ["W", "X", "Y", "Z"].toObservable() .subscribeNext { print($0) } .addDisposableTo(disposeBag)
  24. 24. SUBSCRIBING someObservable.subscribe( onNext: { print("Element: ", $0) }, onError: { print("Error: ", $0) }, onCompleted: { print("Completed") }, onDisposed: { print("Disposed") } ) someObservable .subscribeNext { print("Element: ", $0) }
  25. 25. OPERATORS
  26. 26. MAP
  27. 27. MAP let disposeBag = DisposeBag() Observable.of(1, 2, 3) .map { $0 * 10 } .subscribeNext { print($0) } .addDisposableTo(disposeBag) --- 10 20 30
  28. 28. FILTER
  29. 29. FILTER let disposeBag = DisposeBag() Observable.of(2, 30, 22, 5, 60, 1) .filter { $0 > 10 } .subscribeNext { print($0) } .addDisposableTo(disposeBag) --- 30 22 60
  30. 30. SCAN
  31. 31. SCAN let disposeBag = DisposeBag() Observable.of(1, 2, 3, 4, 5) .scan(0) { aggregateValue, newValue in aggregateValue + newValue } .subscribeNext { print($0) } .addDisposableTo(disposeBag) --- 1 3 6 10 15
  32. 32. MERGE
  33. 33. MERGE let disposeBag = DisposeBag() let subject1 = PublishSubject<String>() let subject2 = PublishSubject<String>() Observable.of(subject1, subject2) .merge() .subscribeNext { print($0) } .addDisposableTo(disposeBag) subject1.onNext("20") subject1.onNext("40") subject1.onNext("60") subject2.onNext("1") subject1.onNext("80") subject1.onNext("100") subject2.onNext("1") 20 40 60 1 80 100 1
  34. 34. RXMARBLES.COM
  35. 35. DISPOSING
  36. 36. .dispose()
  37. 37. .addDisposableTo(disposeBag)
  38. 38. COCOA + RXSWIFT
  39. 39. BINDINGS totCountStepper .rx_value .subscribeNext { value in self.totalNumberOfTots.value = Int(value) } .addDisposableTo(disposeBag)
  40. 40. BINDINGS ➤ NSTextStorage ➤ UIActivityIndicatorView ➤ UIAlertAction ➤ UIApplication ➤ UIBarButtonItem ➤ UIButton ➤ UICollectionView ➤ UIControl ➤ UIDatePicker ➤ UIGestureRecognizer ➤ UIImagePickerController ➤ UIImageView ➤ UILabel ➤ UINavigationItem ➤ UIPageControl ➤ UIPickerView ➤ UIProgressView ➤ UIRefreshControl ➤ UIScrollView ➤ UISearchBar ➤ UISearchController ➤ UISegmentedControl ➤ UISlider ➤ UIStepper ➤ UISwitch ➤ UITabBar ➤ UITabBarItem ➤ UITableView ➤ UITextField ➤ UITextView ➤ UIView ➤ UIViewController
  41. 41. AN EXAMPLE
  42. 42. TATER TOT TIMER
  43. 43. RXSWIFT IN THE WILD
  44. 44. WHERE TO LEARN MORE
  45. 45. WHERE TO LEARN MORE ➤ ReactiveX RxSwift main repo ➤ https://github.com/ReactiveX/RxSwift/ ➤ FRP iOS Learning Resources ➤ https://gist.github.com/JaviLorbada/4a7bd6129275ebefd5a6 ➤ Functional Reactive Programming with RxSwift ➤ https://realm.io/news/slug-max-alexander-functional-reactive-rxswift/ ➤ RxSwift Slack ➤ http://rxswift-slack.herokuapp.com/
  46. 46. AARON DOUGLAS@astralbodies http://astralbodi.es

×