ºÝºÝߣ

ºÝºÝߣShare a Scribd company logo
ReactiveCocoa 101¸Ä
Hai Feng Kao
1
Imperative Programming
Object-Oriented Programming
¡ñ ·âÑb¡¢À^³Ð¡¢¶àÐÍ
¶Uʦ-Áåľ¿¡Â¡
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-effects
o Every time a function is called, it must
return the same y for same x
o 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 Github
o iOS / OSX
o MIT License
o 3.0 supports swift
o https://github.com/ReactiveCocoa/Reacti
veCocoa
Signals
¡ñ Primary data source of FRP
¡ñ Signals send values over time until they
complete
o A signal never sends anything after
completing or erring
o 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
values
o This is core to FRP: data transformation
of values sent over time
o y = f(g(h(x)))
KVO Example
Reactive Example
RACSignals
¡ñ always send id, not primitives
o NSInteger -> NSNumber
o CGRect -> NSValue
Functional Programming
¡ñ The building blocks of functional world
o map, reduce, filter
Map
¡ñ Mapping performs a value transformation
on the given signal
o It ¡±maps¡± each value sent along the
signal to a new value
o 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)
26
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
27
¶Uʦ-Áåľ¿¡Â¡
MVVM(Model View ViewModel)
29
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!!! 30
MVVM(Model View ViewModel)
31
ModelView ViewController ViewModel
ReactiveCocoa
Demo
32
Reference
¡ñ Ash Furrow Reactive Cocoa 101
o https://www.youtube.com/watch?v=TlgU
WYrQ0sc
¡ñ Functional Reactive Programming on iOS
o https://leanpub.com/iosfrp/
Q&A

More Related Content

Reactive cocoa 101¸Ä