Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
Reactive Cocoa e MVVM
Search
Guilherme Martinez Sampaio
October 30, 2014
Programming
1
100
Reactive Cocoa e MVVM
Palestra dada no CocoaHeads CPS Outubro/2013
Guilherme Martinez Sampaio
October 30, 2014
Tweet
Share
More Decks by Guilherme Martinez Sampaio
See All by Guilherme Martinez Sampaio
CocoaPods: Gerenciador de Dependência para iOS e Mac
gsampaio
0
75
Other Decks in Programming
See All in Programming
コーディングは技術者(エンジニア)の嗜みでして / Learning the System Development Mindset from Rock Lady
mackey0225
2
630
UbieのAIパートナーを支えるコンテキストエンジニアリング実践
syucream
2
790
AHC051解法紹介
eijirou
0
640
Ruby Parser progress report 2025
yui_knk
1
250
あのころの iPod を どうにか再生させたい
orumin
2
2.6k
CSC305 Summer Lecture 12
javiergs
PRO
0
130
Kiroの仕様駆動開発から見えてきたAIコーディングとの正しい付き合い方
clshinji
1
180
Trem on Rails - Prompt Engineering com Ruby
elainenaomi
1
100
OSS開発者という働き方
andpad
5
1.6k
ECS初心者の仲間 – TUIツール「e1s」の紹介
keidarcy
0
140
Nuances on Kubernetes - RubyConf Taiwan 2025
envek
0
220
個人軟體時代
ethanhuang13
0
280
Featured
See All Featured
Code Review Best Practice
trishagee
70
19k
RailsConf 2023
tenderlove
30
1.2k
CoffeeScript is Beautiful & I Never Want to Write Plain JavaScript Again
sstephenson
161
15k
[Rails World 2023 - Day 1 Closing Keynote] - The Magic of Rails
eileencodes
36
2.5k
The Illustrated Children's Guide to Kubernetes
chrisshort
48
50k
Intergalactic Javascript Robots from Outer Space
tanoku
272
27k
I Don’t Have Time: Getting Over the Fear to Launch Your Podcast
jcasabona
33
2.4k
Fight the Zombie Pattern Library - RWD Summit 2016
marcelosomers
234
17k
StorybookのUI Testing Handbookを読んだ
zakiyama
30
6.1k
Statistics for Hackers
jakevdp
799
220k
The Myth of the Modular Monolith - Day 2 Keynote - Rails World 2024
eileencodes
26
3k
Become a Pro
speakerdeck
PRO
29
5.5k
Transcript
INTRODUÇÃO A REACTIVE FUNCTIONAL PROGRAMMING. by Guilherme Martinez Sampaio
Guilherme Martinez Sampaio iOS Developer @gsampaio
None
None
Agenda ▸ Functional Reactive Programming ▸ Reactive Cocoa ▸ Model
View View Model ▸ Demo ▸ Referências
FUNCTIONAL REACTIVE PROGRAMMING?
None
FUNCTIONAL REACTIVE PROGRAMMING IS A PROGRAMMING PARADIGM FOR reactive programming
USING THE BUILDING BLOCKS OF functional programming.
FRP HAS BEEN USED FOR PROGRAMMING GUI, ROBOTICS, AND MUSIC,
AIMING TO SIMPLIFY THESE PROBLEMS BY EXPLICITLY modeling time.
EM OUTRAS PALAVRAS DESCREVEMOS NOSSOS PROGRAMAS COMO EVENTOS QUE REAGEM
COM o tempo
Reactive Cocoa
RAC É FORTEMENTE BASEADA NO RXEXTENSIONS DA MICROSOFT PARA .NET
RAC SE BASEIA EM Signals E Sequences
AO INVÉS DE USAR variáveis QUE SÃO ALTERADAS USAMOS sinais
QUE CAPTURA O valor atual E valores futuros.
SINAIS FUNCIONAM COMO Promises
SINAIS TEM subscribers QUE ESCUTAM SUAS ALTERAÇÕES
SUBSCRIBERS RECEBEM 3 eventos
NEXT
COMPLETED
ERROR
RACSIGNAL __block NSUInteger subscribers = 0; RACSignal *signal = [RACSignal
createSignal:^ RACDisposable * (id<RACSubscriber> subscriber) { subscribers++; [subscriber sendNext:@(subscribers)]; [subscriber sendCompleted]; return nil; }]; [signal subscribeNext:^(id x) { NSLog(@"%@", x) } error:^(NSError *error) { NSLog(@"ERRO !"); } completed:^{ NSLog(@"Signal completo"); }] /** Output: 1 Signal completo **/
SINAIS SÃO EXECUTADOS A A PARTIR DO MOMENTO QUE ELE
TEM UM subscriber ATÉ ELE ENVIAR UM SINAL DE completed
SINAIS PODEM TER VÁRIOS subscribers
PODEMOS COMPOR SINAIS USANDO programação funcional
RACSIGNAL __block NSUInteger subscribers = 2; RACSignal *signal = [RACSignal
createSignal:^ RACDisposable * (id<RACSubscriber> subscriber) { subscribers++; [subscriber sendNext:@(subscribers)]; [subscriber sendCompleted]; return nil; }]; RACSignal *powSingal = [signal map:^NSNumber(NSNumber *value){ NSUInteger intValue = [value unsignedIntegerValue]; return @(intValue * intValue); }]; [signal subscribeNext:^(id x) { NSLog(@"VALUE: %@", x); }]; /** Outputs: 9 **/
COLLECTIONS PODEM GERAR RACSequence QUE POR SUA VEZ PODEMOS USAR
APLICAR Programação Funcional
RACSEQUENCE NSArray *lettersArray = [@"A B C D E F
G H I" componentsSeparatedByString:@" "]; RACSignal *letters = lettersArray.rac_sequence.signal; [letters subscribeNext:^(NSString *x) { NSLog(@"%@", x); }]; /** Outputs: A B C D E F G H I **/
RACSEQUENCE RACSignal *numbers = [@[@(1), @(2), @(3)].rac_sequence.signal; RACSignal *pow =
[numbers map:^NSNumber*(NSNumber *value){ NSUInteger intValue = [value unsignedIntegerValue]; return @(intValue * intValue); }]; [pow subscribeNext:^(NSNumber *value) { NSLog(@"%@", value); }]; /** Outputs: 1 4 9 **/
RAC CONTEM MACROS PARA FAZER data-binding
RAC() @interface ViewController () @property(nonatomic) NSUInteger *foo; @property(nonatomic) NSUInteger *bar;
@end @implementation ViewController - (void)viewDidLoad { self.foo = 0; self.bar = 0; NSLog(@"FOO: %@", @(self.foo)); RAC(self, foo) = RACObserve(self, bar); self.bar = 1; NSLog(@"FOO: %@", @(self.foo)); } @end /** Outputs: FOO: 0 FOO: 1 **/
MVVM
MODEL VIEW VIEW-MODEL É UM ARCHITECTURAL PATTERN SEMELHANTE AO MVC
MODEL VIEW CONTROLLER
MAS NA REALIDADE O QUE ACONTECE É QUE FICAMOS COM
view controllers gigantes
MODEL VIEW CONTROLLER
COM MVVM MOVEMOS A LÓGICA DE NEGOCIO TOTALMENTE FORA DO
VC
E TRATAMOS O VC COMO PARTE DA VIEW UMA VEZ
QUE ELE TRATA APRESENTAÇÃO, ROTAÇÃO E NAVEGAÇÃO.
MODEL VIEW VIEW-MODEL
PARA CADA VIEW CRIAMOS UM VIEW MODEL QUE TRATA ELA.
USAMOS DATA BINDING PARA FAZER COMUNICAÇÃO.
MODEL VIEW VIEW MODEL @interface ViewController () @property(nonatomic, strong) ViewModel
*viewModel; @property(nonatomic, weak) UITextField *textField; @property(nonatomic, weak) UILabel *label; @end @implementation ViewController - (void)viewDidLoad { RAC(self.viewModel, text) = [self.textField rac_textSignal]; RAC(self.label, text) = [self.viewModel expensiveComputation]; } @end
VIEW MODELS facilitam escrever testes de uma view. UMA VEZ
QUE TESTAMOS O COMPORTAMENTO DAS VIEWS AO INVÉS DA VIEW EM SI
VIEW MODELS PREGAM Composição sobre Herança.
FAZENDO COM QUE TENHAMOS UM CÓDIGO mais reaproveitável.
UMA VEZ QUE PODEMOS PLUGAR OS VIEW MODELS QUE QUEREMOS
NOS NOSSOS VC
COM ALGUMA EXCESSÕES VIEW MODELS SÃO independentes de plataforma.
DEMO
REFERÊNCIAS ▸ FRP - http://en.wikipedia.org/wiki/ Functional_reactive_programming ▸ Reactive Cocoa -
https://github.com/ ReactiveCocoa/ReactiveCocoa ▸ Model View View Model - http://www.objc.io/ issue-13/mvvm.html
TWITTER ▸ @jspahrsummers ▸ @joshaber ▸ @rob_rix ▸ @indragie ▸
@ashfurrow
SLIDES E DEMO https://github.com/gsampaio/RACMVVMPresentation
THANKS
None