ReactiveCocoa 2.5使用
9051 ワード
ReactiveCocoaベースReactiveCocoa使用詳細ReactiveCocoaブログ
ReactiveCocoaベース
1.信号類RACsignal
2.信号類RACSubject
3.信号類RACReplaySubject
4.集合クラスRACSequenceとRACTuple
5.接続クラスRACMulticastConnection
ReactiveCocoaマクロ
1.@weakifyと@strongify
2.RAC()
3.RACObserve()
ReactiveCocoaステップアップ
1.代替エージェント
2.KVOの代わりに
3.代替通知
4.テキスト・ボックスの傍受
5.コマンドRACCommand
ReactiveCocoaプレミアム
1.マッピングmap:(共通)
2.マッピング信号flattenMap:(新しい信号に変換)
3.バインディング信号bind:(下位コア、一般的には使用しない)
ReactiveCocoa変態
ReactiveCocoaベース
1.信号類RACsignal
//1.
RACSignal *signal = [RACSignal createSignal:^RACDisposable *(id subscriber) {
NSLog(@" ");
//3.
[subscriber sendNext:@"1"];
return [RACDisposable disposableWithBlock:^{
//subscriber
//
//
WJLog(@" ");
}];
}];
//2.
RACDisposable *disposable = [signal subscribeNext:^(id x) {
NSLog(@"%@",x);
}];
//4. 。
[disposable dispose];
2.信号類RACSubject
//RACSubject 。
//1.
RACSubject *subject = [RACSubject subject];
//2.
[subject subscribeNext:^(id x) {
WJLog(@"%@",x);
}];
//
//3.
[subject sendNext:@"1"];
// : , nextBlock。
//
//RACSubject ,
//RACSubject , , nextBlock
3.信号類RACReplaySubject
//1. 。
RACReplaySubject *subject = [RACReplaySubject subject];
//3. 。
[subject sendNext:@"1"];
//2. 。
[subject subscribeNext:^(id x) {
WJLog(@"%@",x);
}];
// , 。
//RACReplaySubject :
//1.
//2. , 。
//RACReplaySubject , 。
//RACReplaySubject *subject = [RACReplaySubject replaySubjectWithCapacity:10];// 。
4.集合クラスRACSequenceとRACTuple
//1.
NSArray *numbers = @[@(1),@(2),@(3),@(4)];
// :
// : RACSequence numbers.rac_sequence
// : RACSequence RACSignal ,numbers.rac_sequence.signal
// : , , , 。
[numbers.rac_sequence.signal subscribeNext:^(id x) {
WJLog(@"%@",x);
}];
//2. , RACTuple( )
NSDictionary *dict = @{@"name":@"xhg",@"age":@(18)};
[dict.rac_sequence.signal subscribeNext:^(id x) {
// , , 。
RACTupleUnpack(NSString *key,NSString *value) = x;
// 。
//NSString *key = x[0];
//NSString *value = x[1];
}];
//3.
// ( )
NSArray *flagArray = [[numbers.rac_sequence map:^id(NSDictionary *value) {
//value: 。
return [Flag flagWithDict:value];
}] array];
5.接続クラスRACMulticastConnection
// ,
//RACMulticastConnection:
//1.
RACSignal *signal = [RACSignal createSignal:^RACDisposable *(id subscriber) {
WJLog(@" ");
//
[subscriber sendNext:@"1"];
return nil;
}];
//2.
RACMulticastConnection *connection = [signal publish];
//3.
[connection.signal subscribeNext:^(id x) {
WJLog(@" %@",x);
}];
[connection.signal subscribeNext:^(id x) {
WJLog(@" %@",x);
}];
//4. ( )
[connection connect];
// block
[connection.signal subscribeNext:^(id x) {
WJLog(@" %@",x);
}];
//
RACMulticastConnection *connect2 = [signal multicast:[RACReplaySubject subject]];
// ( )
[connect2 connect];
[connect2.signal subscribeNext:^(id x) {
WJLog(@" %@",x);
}];
ReactiveCocoaマクロ
1.@weakifyと@strongify
//
@weakify(self);
[self.textField.rac_textSignal subscribeNext:^(id x) {
@strongify(self);
self.label.text = x;
}];
2.RAC()
// , , 。
RAC(self.label,text) = self.textField.rac_textSignal;
3.RACObserve()
//2.RACObserve
//
[RACObserve(self.view, frame) subscribeNext:^(id x) {
}];
ReactiveCocoaステップアップ
1.代替エージェント
//1. 1.RACSubject 2.rac_signalForSelector
// RACSubject
[[_redView rac_signalForSelector:@selector(btnClick:)] subscribeNext:^(id x) {
WJLog(@"btn ");
}];
2.KVOの代わりに
//2. KVO
[[_redView rac_valuesAndChangesForKeyPath:@"frame" options:NSKeyValueObservingOptionNew |NSKeyValueObservingOptionOld |NSKeyValueObservingOptionInitial observer:nil] subscribeNext:^(id x) {
}];
// #import "NSObject+RACKVOWrapper.h"
[_redView rac_observeKeyPath:@"frame" options:NSKeyValueObservingOptionNew observer:nil block:^(id value, NSDictionary *change, BOOL causedByDealloc, BOOL affectedOnlyLastComponent) {
}];
// RACObserve(, )
[RACObserve(_redView, frame) subscribeNext:^(id _Nullable x) {
}];
3.代替通知
//3.
[[[NSNotificationCenter defaultCenter] rac_addObserverForName:UIKeyboardDidShowNotification object:nil] subscribeNext:^(id x) {
}];
4.テキスト・ボックスの傍受
//4.
[textField.rac_textSignal subscribeNext:^(id x) {
}];
5.コマンドRACCommand
//RACCommand:
//RACCommand:
// , 。
//1.
_command = [[RACCommand alloc] initWithSignalBlock:^RACSignal *(id input) {
//input:
//Block : 。
WJLog(@"%@",input);
return [RACSignal createSignal:^RACDisposable *(id subscriber) {
// 。
[subscriber sendNext:@" "];
// 。
[subscriber sendCompleted];
return [RACDisposable disposableWithBlock:^{
WJLog(@" ");
}];
}];
}];
//
// 。
//1. : 。
//2.
RACSignal *signal = [_command execute:@1];
[signal subscribeNext:^(id x) {
WJLog(@"%@",x);
}];
//2. :
// : 。
//executionSignals: , ,signalOfSignals: : 。
[_command.executionSignals subscribeNext:^(RACSignal *x) {
[x subscribeNext:^(id x) {
WJLog(@"%@",x);
}];
}];
// (BOOL)
[_command.executing subscribeNext:^(id x) {
if ([x boolValue] == YES) {
NSLog(@" ");
}else{
NSLog(@" / ");
// _command , 。
}
}];
// :
//switchToLatest , 。( )
//switchToLatest: 。
[_command.executionSignals.switchToLatest subscribeNext:^(id x) {
WJLog(@"%@",x);
}];
[_command execute:@" "];
ReactiveCocoaプレミアム
1.マッピングmap:(共通)
//
RACSubject *subject = [RACSubject subject];
//
RACSignal *signal = [subject map:^id(id value) {
// ,
return [NSString stringWithFormat:@"xmg:%@",value];
}];
//
[signal subscribeNext:^(id x) {
WJLog(@"%@",x);
}];
[subject sendNext:@"1"];
2.マッピング信号flattenMap:(新しい信号に変換)
//
RACSubject *subject = [RACSubject subject];
//
RACSignal *bindSignal = [subject flattenMap:^id(id value) {
//Block :
//value:
value = [NSString stringWithFormat:@"xmg:%@",value];
// , 。
return [RACReturnSignal return:value];
}];
//flattenMap , 。
//
[bindSignal subscribeNext:^(id x) {
WJLog(@"%@",x);
}];
//
[subject sendNext:@"123"];
3.バインディング信号bind:(下位コア、一般的には使用しない)
//1.
RACSubject *subject = [RACSubject subject];
//2.
RACSignal *bindSignal = [subject bind:^RACStreamBindBlock{
return ^RACSignal *(id value, BOOL *stop){
//Block : , Block
//Block :
//value:
WJLog(@" %@",value);
value = [NSString stringWithFormat:@"xmg:%@",value];
// , nil,
//return [RACSignal empty];
return [RACReturnSignal return:value];
};
}];
//3.
[bindSignal subscribeNext:^(id x) {
WJLog(@" %@",x);
}];
//4.
[subject sendNext:@"123"];
ReactiveCocoa変態