SlideShare a Scribd company logo
ReactiveCocoa 101改
Hai Feng Kao
1
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-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
禪師-鈴木俊隆
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

PPTX
Reactive cocoa 101
PDF
Introduction to Monix Coeval
PPTX
Functional Programming in Java
PPT
9781285852744 ppt ch13
PPTX
Massif - the love child of Matlab Simulink and Eclipse
PPT
9781285852744 ppt ch12
PPT
9781285852744 ppt ch01
DOCX
Bti1022 group assignment
Reactive cocoa 101
Introduction to Monix Coeval
Functional Programming in Java
9781285852744 ppt ch13
Massif - the love child of Matlab Simulink and Eclipse
9781285852744 ppt ch12
9781285852744 ppt ch01
Bti1022 group assignment

What's hot (20)

PPT
9781285852744 ppt ch10
PPT
9781285852744 ppt ch17
PPT
9781285852744 ppt ch08
PPTX
Lesson 13 object and class
PPTX
Graphics Libraries
PDF
Lesson 5.2 logical operators
PPT
9781285852744 ppt ch18
PDF
Functional Scala
PDF
steppy: lightweight, open source, Python library for fast and reproducible ex...
PDF
Lesson 4.1 completing the problem solving process
PPT
9781285852744 ppt ch16
PDF
OpenDataPlane Testing in Travis
PDF
CMSC 350 PROJECT 1
PDF
Lesson 4.2 5th and 6th step
PPTX
Ddd melbourne 2011 C# async ctp
PDF
Falling into that State (of) Flow: Easy State Management
PPTX
Java Decompiler
PDF
Net framework
PPTX
RxJS vs RxJava: Intro
PDF
Drupal and testing (2010 - 2011 / 2)
9781285852744 ppt ch10
9781285852744 ppt ch17
9781285852744 ppt ch08
Lesson 13 object and class
Graphics Libraries
Lesson 5.2 logical operators
9781285852744 ppt ch18
Functional Scala
steppy: lightweight, open source, Python library for fast and reproducible ex...
Lesson 4.1 completing the problem solving process
9781285852744 ppt ch16
OpenDataPlane Testing in Travis
CMSC 350 PROJECT 1
Lesson 4.2 5th and 6th step
Ddd melbourne 2011 C# async ctp
Falling into that State (of) Flow: Easy State Management
Java Decompiler
Net framework
RxJS vs RxJava: Intro
Drupal and testing (2010 - 2011 / 2)
Ad

Similar to Reactive cocoa 101改 (20)

PDF
ReactiveCocoa - Functional Reactive Programming concepts in iOS
PDF
Reactive Cocoa
PDF
Petr Šíma: Principy funkcionálně reaktivního programovaní na mobilních platfo...
PDF
Reactive cocoa cocoaheadsbe_2014
PDF
Introduction to Functional Reactive Programming
PDF
Introduction To Functional Reactive Programming Poznan
PDF
Code europe
PDF
Buy ebook Functional Reactive Programming 1st Edition Stephen Blackheath chea...
PDF
(Functional) reactive programming (@pavlobaron)
PPT
Reactive cocoa
PDF
Functional Reactive Programming
PDF
Tech fest
PPTX
Why functional programming in C# & F#
PPTX
Functional reactive programming
PPTX
Real world functional reactive programming
PDF
Functional Reactive Programming in Clojurescript
PDF
Moving towards Reactive Programming
PDF
Functional Reactive Programming / Compositional Event Systems
PDF
Functional programming
ReactiveCocoa - Functional Reactive Programming concepts in iOS
Reactive Cocoa
Petr Šíma: Principy funkcionálně reaktivního programovaní na mobilních platfo...
Reactive cocoa cocoaheadsbe_2014
Introduction to Functional Reactive Programming
Introduction To Functional Reactive Programming Poznan
Code europe
Buy ebook Functional Reactive Programming 1st Edition Stephen Blackheath chea...
(Functional) reactive programming (@pavlobaron)
Reactive cocoa
Functional Reactive Programming
Tech fest
Why functional programming in C# & F#
Functional reactive programming
Real world functional reactive programming
Functional Reactive Programming in Clojurescript
Moving towards Reactive Programming
Functional Reactive Programming / Compositional Event Systems
Functional programming
Ad

Reactive cocoa 101改