infinum ios talks #4 - making our viper more reactive

45
Reactive VIPER IVAN ĐIKIĆ

Upload: infinum

Post on 08-Apr-2017

150 views

Category:

Technology


0 download

TRANSCRIPT

Page 1: Infinum iOS Talks #4 - Making our VIPER more reactive

Reactive VIPER

IVAN ĐIKIĆ

Page 2: Infinum iOS Talks #4 - Making our VIPER more reactive

WHY?

Page 3: Infinum iOS Talks #4 - Making our VIPER more reactive

UIVIEWCONTROLLER (UIVIEW)

Page 4: Infinum iOS Talks #4 - Making our VIPER more reactive

• 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

VIPER

Page 6: Infinum iOS Talks #4 - Making our VIPER more reactive

• 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

• 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

• View / View Controller

• Presenter (view model :))

• Interactor

• Wireframe

MAIN COMPONENTS

Page 9: Infinum iOS Talks #4 - Making our VIPER more reactive

MAIN COMPONENTS

Page 10: Infinum iOS Talks #4 - Making our VIPER more reactive

MAIN COMPONENTS

Page 11: Infinum iOS Talks #4 - Making our VIPER more reactive

• business logic agnostic

• knows how to layout itself

• reusable

VIEW / VIEW CONTROLLER

Page 12: Infinum iOS Talks #4 - Making our VIPER more reactive

• 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

• has the data

• knows how to get the data

INTERACTOR

Page 14: Infinum iOS Talks #4 - Making our VIPER more reactive

• knows how to navigate

• knows how to create other VIPER modules

WIREFRAME

Page 15: Infinum iOS Talks #4 - Making our VIPER more reactive

RX(SWIFT)

Page 16: Infinum iOS Talks #4 - Making our VIPER more reactive

• 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

• 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

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

ITERATOR, OBSERVER

Page 20: Infinum iOS Talks #4 - Making our VIPER more reactive

Observer and Iterator patterns are mathematical duals

CORE KNOWLEDGE

Page 21: Infinum iOS Talks #4 - Making our VIPER more reactive

ITERATOR PATTERN

Page 22: Infinum iOS Talks #4 - Making our VIPER more reactive

• 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

• a collection of protocols

• CollectionType

• SequenceType

• GeneratorType

ITERATOR IN SWIFT

Page 24: Infinum iOS Talks #4 - Making our VIPER more reactive

GeneratorType - serve elements

protocol GeneratorType { associatedtype Element mutating func next() -> Element? }

Page 25: Infinum iOS Talks #4 - Making our VIPER more reactive

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

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

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

OBSERVER PATTERN

Page 29: Infinum iOS Talks #4 - Making our VIPER more reactive

• 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

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

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

OBSERVABLES AKA

SEQUENCES

Page 33: Infinum iOS Talks #4 - Making our VIPER more reactive

• 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

• 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

TIPS’N’TRICKS

Page 36: Infinum iOS Talks #4 - Making our VIPER more reactive

RTFM

Page 37: Infinum iOS Talks #4 - Making our VIPER more reactive

AVOID NESTING CALLS

Page 38: Infinum iOS Talks #4 - Making our VIPER more reactive

ALWAYS USE `DISPOSE BAG`

Page 39: Infinum iOS Talks #4 - Making our VIPER more reactive

`SOURCEKIT` HAS CRASHED

Page 40: Infinum iOS Talks #4 - Making our VIPER more reactive

USE [WEAK SELF]

Page 41: Infinum iOS Talks #4 - Making our VIPER more reactive

PERIODICALLY PRINT RESOURCES COUNT

Page 42: Infinum iOS Talks #4 - Making our VIPER more reactive

STRIVE TO MODEL YOUR SYSTEMS AS PURE FUNCTIONS

Page 43: Infinum iOS Talks #4 - Making our VIPER more reactive

RESOURCES

Page 44: Infinum iOS Talks #4 - Making our VIPER more reactive

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

Q&A