34
ReactiveCocoa 101 改 Hai Feng Kao

Reactive cocoa 101改

Embed Size (px)

Citation preview

ReactiveCocoa 101改Hai Feng Kao

Imperative Programming

Object-Oriented Programming● 封裝、繼承、多型

禪師 -鈴木俊隆

Why is debugging difficult?

Functional Programming● Lambda Calculus● Functions without side effects● Programs as function

compositions (y = f(g(h(x))))

Functional Reactive Programming● In functional programming, things get hard

because functions can’t have side-effectso Every time a function is called, it must

return the same y for same xo y = f(x)

● In functional reactive programming, time is an implicit parameter (no monads)

o y = f(x, t)

ReactiveCocoa● A open source FRP framework developed

by Githubo iOS / OSXo MIT Licenseo 3.0 supports swifto https://github.com/ReactiveCocoa/Reacti

veCocoa

Signals● Primary data source of FRP● Signals send values over time until they

completeo A signal never sends anything after

completing or erringo A signal either completes, or errors out,

but never both

Signals(cont.)● A signal has no concept of a “current

value” or “past value”● It’s like a pipe● Signals can be chained to transform the

valueso This is core to FRP: data transformation

of values sent over timeo y = f(g(h(x)))

KVO Example

Reactive Example

RACSignals● always send id, not primitives

o NSInteger -> NSNumbero CGRect -> NSValue

Functional Programming● The building blocks of functional world

o map, reduce, filter

Map● Mapping performs a value transformation

on the given signalo It ”maps” each value sent along the

signal to a new valueo It then passes that new value along,

creating a new signal

Example

● Combines two or more signals’ values into one

o Useful for waiting for conditions to be met

o Useful for deciding between values

Reduce

Example

Filter● Filtering allows values that pass some test

to “pass through”

Example

Functional Programming● The building blocks of functional world

o map, reduce, filter

Functional Programming● The building blocks of functional world

o map, reduce, filter

USELESS?

Bindings● Bindings can bind a property to the latest

value sent on a signal● All bindings created with the RAC macro

are one-way● Bindings are the secret sauce that hold

apps together

Example

Imperative vs Functional

Imperative:

Functional:

MVC (Model View Controller)

ModelView Controller

Input

Update

Update

Notify

MVC (Model View Controller)• Pros :

• Independent module• Reusable model

• Cons:• too may files• hard to write unit test• controller need to access view&model

禪師 -鈴木俊隆

MVVM(Model View ViewModel)

ModelView ViewModel

Input Update

MVVM(Model View ViewModel)• Pros:

• ViewModel can be unit tested• ViewModel has no access to view• Model has no access to ViewModel

• View is simple• UI logic moved to ViewModel

• Cons:• hard to debug• performance hit!!!

MVVM(Model View ViewModel)

ModelView ViewController ViewModel

ReactiveCocoa

Demo

Reference● Ash Furrow Reactive Cocoa 101

o https://www.youtube.com/watch?v=TlgUWYrQ0sc

● Functional Reactive Programming on iOSo https://leanpub.com/iosfrp/

Q&A