infinum ios talks #4 - making our viper more reactive
TRANSCRIPT
![Page 1: Infinum iOS Talks #4 - Making our VIPER more reactive](https://reader031.vdocument.in/reader031/viewer/2022030307/58e8d2741a28abb3398b58db/html5/thumbnails/1.jpg)
Reactive VIPER
IVAN ĐIKIĆ
![Page 2: Infinum iOS Talks #4 - Making our VIPER more reactive](https://reader031.vdocument.in/reader031/viewer/2022030307/58e8d2741a28abb3398b58db/html5/thumbnails/2.jpg)
WHY?
![Page 3: Infinum iOS Talks #4 - Making our VIPER more reactive](https://reader031.vdocument.in/reader031/viewer/2022030307/58e8d2741a28abb3398b58db/html5/thumbnails/3.jpg)
UIVIEWCONTROLLER (UIVIEW)
![Page 4: Infinum iOS Talks #4 - Making our VIPER more reactive](https://reader031.vdocument.in/reader031/viewer/2022030307/58e8d2741a28abb3398b58db/html5/thumbnails/4.jpg)
• workhorse
• hard to test
• hard to maintain
• not reusable
BUILDING BLOCK FOR COCOA TOUCH
![Page 5: Infinum iOS Talks #4 - Making our VIPER more reactive](https://reader031.vdocument.in/reader031/viewer/2022030307/58e8d2741a28abb3398b58db/html5/thumbnails/5.jpg)
VIPER
![Page 6: Infinum iOS Talks #4 - Making our VIPER more reactive](https://reader031.vdocument.in/reader031/viewer/2022030307/58e8d2741a28abb3398b58db/html5/thumbnails/6.jpg)
• in 4 parts
• good for medium to big size applications
• good separation of responsibility
• loosely coupled components
SPLIT UP VIEW CONTROLLER
![Page 7: Infinum iOS Talks #4 - Making our VIPER more reactive](https://reader031.vdocument.in/reader031/viewer/2022030307/58e8d2741a28abb3398b58db/html5/thumbnails/7.jpg)
• connection between components is achieved using protocols
• composition instead inheritance in most cases
• most of UI is done in Storyboards but without Segues
GENERAL
![Page 8: Infinum iOS Talks #4 - Making our VIPER more reactive](https://reader031.vdocument.in/reader031/viewer/2022030307/58e8d2741a28abb3398b58db/html5/thumbnails/8.jpg)
• View / View Controller
• Presenter (view model :))
• Interactor
• Wireframe
MAIN COMPONENTS
![Page 9: Infinum iOS Talks #4 - Making our VIPER more reactive](https://reader031.vdocument.in/reader031/viewer/2022030307/58e8d2741a28abb3398b58db/html5/thumbnails/9.jpg)
MAIN COMPONENTS
![Page 10: Infinum iOS Talks #4 - Making our VIPER more reactive](https://reader031.vdocument.in/reader031/viewer/2022030307/58e8d2741a28abb3398b58db/html5/thumbnails/10.jpg)
MAIN COMPONENTS
![Page 11: Infinum iOS Talks #4 - Making our VIPER more reactive](https://reader031.vdocument.in/reader031/viewer/2022030307/58e8d2741a28abb3398b58db/html5/thumbnails/11.jpg)
• business logic agnostic
• knows how to layout itself
• reusable
VIEW / VIEW CONTROLLER
![Page 12: Infinum iOS Talks #4 - Making our VIPER more reactive](https://reader031.vdocument.in/reader031/viewer/2022030307/58e8d2741a28abb3398b58db/html5/thumbnails/12.jpg)
• business logic
• least reusable part
• has reference to
• view
• interactor
• wireframe
• models data received from interactor
• views delegate (responds to view events)
PRESENTER
![Page 13: Infinum iOS Talks #4 - Making our VIPER more reactive](https://reader031.vdocument.in/reader031/viewer/2022030307/58e8d2741a28abb3398b58db/html5/thumbnails/13.jpg)
• has the data
• knows how to get the data
INTERACTOR
![Page 14: Infinum iOS Talks #4 - Making our VIPER more reactive](https://reader031.vdocument.in/reader031/viewer/2022030307/58e8d2741a28abb3398b58db/html5/thumbnails/14.jpg)
• knows how to navigate
• knows how to create other VIPER modules
WIREFRAME
![Page 15: Infinum iOS Talks #4 - Making our VIPER more reactive](https://reader031.vdocument.in/reader031/viewer/2022030307/58e8d2741a28abb3398b58db/html5/thumbnails/15.jpg)
RX(SWIFT)
![Page 16: Infinum iOS Talks #4 - Making our VIPER more reactive](https://reader031.vdocument.in/reader031/viewer/2022030307/58e8d2741a28abb3398b58db/html5/thumbnails/16.jpg)
• FRP
• functional programming
• functions don’t have side effects, no mutable state
• difficult to program since real world is all about side effects
• reactive programming
• changes propagate throughout a system automatically
• functional reactive programming
• we model user input as a function that changes over time,
abstracting away the idea of mutable state
CREATED BY MICROSOFT (RX.NET)
![Page 17: Infinum iOS Talks #4 - Making our VIPER more reactive](https://reader031.vdocument.in/reader031/viewer/2022030307/58e8d2741a28abb3398b58db/html5/thumbnails/17.jpg)
• sweet spot between functional and imperative programming world
• unifies various design patterns (GoF)
• Delegate
• Callback (Closures, Blocks)
• KVO
• Notifications
• Observer
• enables building apps in declarative style
WHY RX?
![Page 18: Infinum iOS Talks #4 - Making our VIPER more reactive](https://reader031.vdocument.in/reader031/viewer/2022030307/58e8d2741a28abb3398b58db/html5/thumbnails/18.jpg)
DECLARATIVE STYLE
Observable .combineLatest
( firstName.rx_text, lastName.rx_text
) {
$0 + " " + $1 }
.map { "Greetings, \($0)"
} .bindTo(greetingLabel.rx_text)
![Page 19: Infinum iOS Talks #4 - Making our VIPER more reactive](https://reader031.vdocument.in/reader031/viewer/2022030307/58e8d2741a28abb3398b58db/html5/thumbnails/19.jpg)
ITERATOR, OBSERVER
![Page 20: Infinum iOS Talks #4 - Making our VIPER more reactive](https://reader031.vdocument.in/reader031/viewer/2022030307/58e8d2741a28abb3398b58db/html5/thumbnails/20.jpg)
Observer and Iterator patterns are mathematical duals
CORE KNOWLEDGE
![Page 21: Infinum iOS Talks #4 - Making our VIPER more reactive](https://reader031.vdocument.in/reader031/viewer/2022030307/58e8d2741a28abb3398b58db/html5/thumbnails/21.jpg)
ITERATOR PATTERN
![Page 22: Infinum iOS Talks #4 - Making our VIPER more reactive](https://reader031.vdocument.in/reader031/viewer/2022030307/58e8d2741a28abb3398b58db/html5/thumbnails/22.jpg)
• pull based interface
• good for traversing collections
• standard interface
• generator
• next (pull)
• no more data
• throw (error)
• arrays, dictionaries, sets
ITERATOR
![Page 23: Infinum iOS Talks #4 - Making our VIPER more reactive](https://reader031.vdocument.in/reader031/viewer/2022030307/58e8d2741a28abb3398b58db/html5/thumbnails/23.jpg)
• a collection of protocols
• CollectionType
• SequenceType
• GeneratorType
ITERATOR IN SWIFT
![Page 24: Infinum iOS Talks #4 - Making our VIPER more reactive](https://reader031.vdocument.in/reader031/viewer/2022030307/58e8d2741a28abb3398b58db/html5/thumbnails/24.jpg)
GeneratorType - serve elements
protocol GeneratorType { associatedtype Element mutating func next() -> Element? }
![Page 25: Infinum iOS Talks #4 - Making our VIPER more reactive](https://reader031.vdocument.in/reader031/viewer/2022030307/58e8d2741a28abb3398b58db/html5/thumbnails/25.jpg)
SequenceType - knows how to create generator struct PrefixSequence: SequenceType { let string: String func generate() -> PrefixGenerator { return PrefixGenerator(string: string) } }
![Page 26: Infinum iOS Talks #4 - Making our VIPER more reactive](https://reader031.vdocument.in/reader031/viewer/2022030307/58e8d2741a28abb3398b58db/html5/thumbnails/26.jpg)
CollectionType
- repeatable iteration - access to the elements via index
struct BeersCollection<T> { let beers: [T] }
![Page 27: Infinum iOS Talks #4 - Making our VIPER more reactive](https://reader031.vdocument.in/reader031/viewer/2022030307/58e8d2741a28abb3398b58db/html5/thumbnails/27.jpg)
CollectionType extension BeersCollection: SequenceType {
typealias Generator = AnyGenerator<T>
func generate() -> AnyGenerator<T> { var i = 0
return AnyGenerator { i += 1 return i >= self.beers.count ? nil : self.beers[i] } } }
![Page 28: Infinum iOS Talks #4 - Making our VIPER more reactive](https://reader031.vdocument.in/reader031/viewer/2022030307/58e8d2741a28abb3398b58db/html5/thumbnails/28.jpg)
OBSERVER PATTERN
![Page 29: Infinum iOS Talks #4 - Making our VIPER more reactive](https://reader031.vdocument.in/reader031/viewer/2022030307/58e8d2741a28abb3398b58db/html5/thumbnails/29.jpg)
• push based interface
• good for UI events
• missing
• error handling
• no more data
OBSERVER
![Page 30: Infinum iOS Talks #4 - Making our VIPER more reactive](https://reader031.vdocument.in/reader031/viewer/2022030307/58e8d2741a28abb3398b58db/html5/thumbnails/30.jpg)
Observer
protocol PropertyObserver : class { func willChangePropertyName(propertyName: String, newPropertyValue: AnyObject?) func didChangePropertyName(propertyName: String, oldPropertyValue: AnyObject?) }
![Page 31: Infinum iOS Talks #4 - Making our VIPER more reactive](https://reader031.vdocument.in/reader031/viewer/2022030307/58e8d2741a28abb3398b58db/html5/thumbnails/31.jpg)
Observer
class TestChambers {
weak var observer: PropertyObserver?
var testChamberNumber: Int = 0 { willSet(newValue) { observer?.willChangePropertyName("testChamberNumber", newPropertyValue:newValue) } didSet { observer?.didChangePropertyName("testChamberNumber", oldPropertyValue:oldValue) } } }
![Page 32: Infinum iOS Talks #4 - Making our VIPER more reactive](https://reader031.vdocument.in/reader031/viewer/2022030307/58e8d2741a28abb3398b58db/html5/thumbnails/32.jpg)
OBSERVABLES AKA
SEQUENCES
![Page 33: Infinum iOS Talks #4 - Making our VIPER more reactive](https://reader031.vdocument.in/reader031/viewer/2022030307/58e8d2741a28abb3398b58db/html5/thumbnails/33.jpg)
• unifying Iterator and Observer pattern
• can be composed
• we already know how to do operations on collections
• map
• filter
• reduce
• the equivalence of observer pattern (Observable<Element>
sequence) and normal sequences (SequenceType) is the most
important thing to understand about Rx
GENERAL
![Page 34: Infinum iOS Talks #4 - Making our VIPER more reactive](https://reader031.vdocument.in/reader031/viewer/2022030307/58e8d2741a28abb3398b58db/html5/thumbnails/34.jpg)
• every observable sequence is just a sequence
• push interface (aka callback)
• regex
• next* (error | completed)?
• can have 0 or more elements
• once error or completed
• sequence cannot produce any other element
• all internal resources will be disposed
• no work will be performed until you call subscribe
OBSERVABLES AKA SEQUENCES
![Page 35: Infinum iOS Talks #4 - Making our VIPER more reactive](https://reader031.vdocument.in/reader031/viewer/2022030307/58e8d2741a28abb3398b58db/html5/thumbnails/35.jpg)
TIPS’N’TRICKS
![Page 36: Infinum iOS Talks #4 - Making our VIPER more reactive](https://reader031.vdocument.in/reader031/viewer/2022030307/58e8d2741a28abb3398b58db/html5/thumbnails/36.jpg)
RTFM
![Page 37: Infinum iOS Talks #4 - Making our VIPER more reactive](https://reader031.vdocument.in/reader031/viewer/2022030307/58e8d2741a28abb3398b58db/html5/thumbnails/37.jpg)
AVOID NESTING CALLS
![Page 38: Infinum iOS Talks #4 - Making our VIPER more reactive](https://reader031.vdocument.in/reader031/viewer/2022030307/58e8d2741a28abb3398b58db/html5/thumbnails/38.jpg)
ALWAYS USE `DISPOSE BAG`
![Page 39: Infinum iOS Talks #4 - Making our VIPER more reactive](https://reader031.vdocument.in/reader031/viewer/2022030307/58e8d2741a28abb3398b58db/html5/thumbnails/39.jpg)
`SOURCEKIT` HAS CRASHED
![Page 40: Infinum iOS Talks #4 - Making our VIPER more reactive](https://reader031.vdocument.in/reader031/viewer/2022030307/58e8d2741a28abb3398b58db/html5/thumbnails/40.jpg)
USE [WEAK SELF]
![Page 41: Infinum iOS Talks #4 - Making our VIPER more reactive](https://reader031.vdocument.in/reader031/viewer/2022030307/58e8d2741a28abb3398b58db/html5/thumbnails/41.jpg)
PERIODICALLY PRINT RESOURCES COUNT
![Page 42: Infinum iOS Talks #4 - Making our VIPER more reactive](https://reader031.vdocument.in/reader031/viewer/2022030307/58e8d2741a28abb3398b58db/html5/thumbnails/42.jpg)
STRIVE TO MODEL YOUR SYSTEMS AS PURE FUNCTIONS
![Page 43: Infinum iOS Talks #4 - Making our VIPER more reactive](https://reader031.vdocument.in/reader031/viewer/2022030307/58e8d2741a28abb3398b58db/html5/thumbnails/43.jpg)
RESOURCES
![Page 44: Infinum iOS Talks #4 - Making our VIPER more reactive](https://reader031.vdocument.in/reader031/viewer/2022030307/58e8d2741a28abb3398b58db/html5/thumbnails/44.jpg)
RESOURCES
• RxSwift • Reactive Programming Overview • Design Patterns In Swift • Inside .Net Reactive Framework • Duality • Visual Operators (RxMarbles)
![Page 45: Infinum iOS Talks #4 - Making our VIPER more reactive](https://reader031.vdocument.in/reader031/viewer/2022030307/58e8d2741a28abb3398b58db/html5/thumbnails/45.jpg)
Q&A