infinum ios talks #4 - making our viper more reactive
TRANSCRIPT
Reactive VIPER
IVAN ĐIKIĆ
WHY?
UIVIEWCONTROLLER (UIVIEW)
• workhorse
• hard to test
• hard to maintain
• not reusable
BUILDING BLOCK FOR COCOA TOUCH
VIPER
• in 4 parts
• good for medium to big size applications
• good separation of responsibility
• loosely coupled components
SPLIT UP VIEW CONTROLLER
• connection between components is achieved using protocols
• composition instead inheritance in most cases
• most of UI is done in Storyboards but without Segues
GENERAL
• View / View Controller
• Presenter (view model :))
• Interactor
• Wireframe
MAIN COMPONENTS
MAIN COMPONENTS
MAIN COMPONENTS
• business logic agnostic
• knows how to layout itself
• reusable
VIEW / VIEW CONTROLLER
• business logic
• least reusable part
• has reference to
• view
• interactor
• wireframe
• models data received from interactor
• views delegate (responds to view events)
PRESENTER
• has the data
• knows how to get the data
INTERACTOR
• knows how to navigate
• knows how to create other VIPER modules
WIREFRAME
RX(SWIFT)
• 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)
• 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?
DECLARATIVE STYLE
Observable .combineLatest
( firstName.rx_text, lastName.rx_text
) {
$0 + " " + $1 }
.map { "Greetings, \($0)"
} .bindTo(greetingLabel.rx_text)
ITERATOR, OBSERVER
Observer and Iterator patterns are mathematical duals
CORE KNOWLEDGE
ITERATOR PATTERN
• pull based interface
• good for traversing collections
• standard interface
• generator
• next (pull)
• no more data
• throw (error)
• arrays, dictionaries, sets
ITERATOR
• a collection of protocols
• CollectionType
• SequenceType
• GeneratorType
ITERATOR IN SWIFT
GeneratorType - serve elements
protocol GeneratorType { associatedtype Element mutating func next() -> Element? }
SequenceType - knows how to create generator struct PrefixSequence: SequenceType { let string: String func generate() -> PrefixGenerator { return PrefixGenerator(string: string) } }
CollectionType
- repeatable iteration - access to the elements via index
struct BeersCollection<T> { let beers: [T] }
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] } } }
OBSERVER PATTERN
• push based interface
• good for UI events
• missing
• error handling
• no more data
OBSERVER
Observer
protocol PropertyObserver : class { func willChangePropertyName(propertyName: String, newPropertyValue: AnyObject?) func didChangePropertyName(propertyName: String, oldPropertyValue: AnyObject?) }
Observer
class TestChambers {
weak var observer: PropertyObserver?
var testChamberNumber: Int = 0 { willSet(newValue) { observer?.willChangePropertyName("testChamberNumber", newPropertyValue:newValue) } didSet { observer?.didChangePropertyName("testChamberNumber", oldPropertyValue:oldValue) } } }
OBSERVABLES AKA
SEQUENCES
• 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
• 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
TIPS’N’TRICKS
RTFM
AVOID NESTING CALLS
ALWAYS USE `DISPOSE BAG`
`SOURCEKIT` HAS CRASHED
USE [WEAK SELF]
PERIODICALLY PRINT RESOURCES COUNT
STRIVE TO MODEL YOUR SYSTEMS AS PURE FUNCTIONS
RESOURCES
RESOURCES
• RxSwift • Reactive Programming Overview • Design Patterns In Swift • Inside .Net Reactive Framework • Duality • Visual Operators (RxMarbles)
Q&A