์ทจ์ ํ ์์๋ ๋ช ๋ น์ ๋ํ ์ ์ ํ ํจํด์ ์ฐพ๊ณ ์์ต๋๋ค.
์๋๋ฆฌ์ค๋ ๋ค์๊ณผ ๊ฐ์ต๋๋ค.
Joe ์ฌ์ฉ์๋ ์ฅ์น์์ ํธ์ํฐ ์๋น์ค์ ๋ก๊ทธ์ธํ๋ ค๊ณ ํฉ๋๋ค. ํ๋ฉด์ ํธ์ํฐ๋ผ๋ ๋ฒํผ์ด ์์ผ๋ฉฐ ์ฌ์ฉ์๊ฐ ํญํ๋ฉด ๋ก๊ทธ์ธ ํ๋ก์ธ์ค๊ฐ ์์๋๊ณ ๋ฒํผ์ ํ
์คํธ๊ฐ ์ทจ์๋ก ๋ณ๊ฒฝ๋ฉ๋๋ค. ๋ก๊ทธ์ธ ํ๋ก์ธ์ค์ ์ค๋ ์๊ฐ (์ : 30 ์ด)์ด ์์๋ ์ ์์ผ๋ฉฐ์ด ์๊ฐ ๋์ ์ฌ์ฉ์๊ฐ ๋ฒํผ์ ๋ค์ ๋๋ฅด๋ฉด ๊ธฐ์กด ๋ก๊ทธ์ธ์ด ์ทจ์๋ฉ๋๋ค.
์ด ์์ ์ ์ํํ๊ธฐ ์ํด ์ฌ๋ฌ ๊ฐ์ง ๋ฐฉ๋ฒ์ ์๋ํ์ง๋ง ๋ชจ๋ ๋งค์ฐ ๋์ ๋์๊ฐ๋ฉ๋๋ค. ์ง๊ธ๊น์ง ๋ด๊ฐ ์๊ฐ ํด๋ธ ์ต๊ณ ์ ์๋ฃจ์ ์ ๋ค์๊ณผ ๊ฐ์ต๋๋ค.
<strong i="9">@property</strong> (strong, nonatomic) RACCommand *twitterLoginCommand;
<strong i="10">@property</strong> (strong, nonatomic) RACCommand *cancelCommand;
<strong i="11">@property</strong> (strong, nonatomic) id authenticatedUser;
<strong i="12">@property</strong> (weak, nonatomic) RACDisposable * authenticationDisposable;
-(void) viewDidLoad {
RAC(self, twitterButton.rac_command, self.twitterLoginCommand) = [[RACObserve(self, twitterLoginCommand.executing) flatten]
map:^id(NSNumber * value) {
@strongify(self);
if (value.boolValue) {
return self.cancelCommand;
} else {
return self.twitterLoginCommand;
}
}];
}
-(RACCommand *) cancelCommand {
if (!_cancelCommand) {
@weakify(self);
_cancelCommand = [[RACCommand alloc] initWithSignalBlock:^RACSignal *(id input) {
@strongify(self);
[self.authenticationDisposable dispose];
return [RACSignal empty];
}];
}
return _cancelCommand;
}
- (RACCommand *)twitterLoginCommand {
if (!_twitterLoginCommand) {
@weakify(self);
_twitterLoginCommand = [[RACCommand alloc]initWithSignalBlock: ^RACSignal *(UIButton *button) {
@strongify(self);
RACSignal * signal = [self twitterSignInSignal];
self.authenticationDisposable = [signal subscribeNext:^(FAUser * user) {
self.authenticatedUser = user;
}error:^(NSError *error) {
self.authenticationDisposable = nil;
}];
return signal;
}];
}
return _twitterLoginCommand;
}
- (RACSignal *)twitterSignInSignal {
//Left out, returns a signal with a user
}
์๋๋ viewDidLoad์์ twitter ๋ช ๋ น์ ๊ตฌ๋ ํ๊ณ ์ทจ์ ์ ํธ์ takeUntil์ ์ฌ์ฉํ์ง๋ง (์ฒ๋ถ ํ ์ ์๊ณ ๋ช ๋ น์ผ๋ก ๊ตฌ๋ ํ๋ ๋์ ) ๋ช ๋ น์ด ์ทจ์ ์ฆ์ ์คํ๋๋ ๊ฒ์ ์ค์งํ์ง ์๋ ๊ฒ ๊ฐ์ต๋๋ค (๋ก๊ทธ์ธ ์์ ์ ํธ๊ฐ takeUntil์ ์ฒ๋ฆฌ ํ ๋๊น์ง ๊ณ์ ์คํ๋ฉ๋๋ค.)
์ด๊ฑธ๋ก ํด๋ด.
_twitterLoginCommand = [[RACCommand alloc] initWithSignalBlock:^(id _) {
@strongify(self);
return [[self
twitterSignInSignal]
takeUntil:self.cancelCommand.executionSignals];
}];
RAC(self.authenticatedUser) = [self.twitterLoginCommand.executionSignals switchToLatest];
๋ช ๋ น ์์ฑ์ init ๋ฉ์๋๋ก ์ด๋ํ๋ ๊ฒ์ด ์ข์ต๋๋ค.
๊ทธ๊ฒ์ ๋งค๋ ฅ์ฒ๋ผ ์๋ํ๊ณ ๋ชจ๋ ๊ฒ์ด ์ข์ ๋์์ ๋๋ค. ๊ณ ๋ง์์, ์ ๊ฐ์๋๋งํผํ๋๋ฐ ๋ฉฐ์น ์ด ๊ฑธ ๋ ธ์ด์. ์ง๋๋ฒ์ ๊ทธ๋ฐ ์ ๊ทผ์ ์๋ํ์ ๋ ๋ญ๊ฐ ๋์น ๊ฒ ํ๋ฆผ ์์ด์.
์ฒ๋ง์์!
RAC (self.authenticatedUser) = [self.twitterLoginCommand.executionSignals switchToLatest];
๊ทธ๊ฒ์ ๋ณ๊ฒฝํ๋ฉด
RAC (self.authenticatedUser) = [self.twitterLoginCommand.executionSignals flatten];
๋ ๋์? ๋ฟก๋ฟก
๊ฐ์ฅ ์ ์ฉํ ๋๊ธ
์ด๊ฑธ๋ก ํด๋ด.
๋ช ๋ น ์์ฑ์ init ๋ฉ์๋๋ก ์ด๋ํ๋ ๊ฒ์ด ์ข์ต๋๋ค.