RAC学习笔记

前言

最近,ReactiveCocoa火的不行,网上各种大牛各种说RAC的优点,早就有学习的想法,一直没有付诸行动,最近公司开发不是很紧张,特意学习了一下RAC,以便以后改版使用。

1.[-subscribe…][RACSignal]该方法会检测RACSignal中当前和以后的值,来执行subscribe中相应的方法。

example:

RACSignal *letters = [@“a b c d e f” componentsSeparatedByString:@“ “].rac_sequence.signal;

//数组中的元素分割成多个信号

[letters subscribeNext:^(id x){

NSLog(@“%@“, x);

}];

//letters信号的subscriber在signal的值改变时执行相应方法

output:a b c d e f(每一行输出一个字母)

2.[-subscribe…][RACSignal]

__block unsigned subscriptions =0;RACSignal *loggingSignal = [RACSignal createSignal:^ RACDisposable * (id subscriber) {    subscriptions++;    [subscriber sendCompleted];returnnil;}];

// Outputs:// subscription 1[loggingSignal subscribeCompleted:^{NSLog(@"subscription %u", subscriptions);}];// Outputs:// subscription 2[loggingSignal subscribeCompleted:^{NSLog(@"subscription %u", subscriptions);}];

执行顺序:创建RACSignal ->执行subscribeCompleted->回调createSignal中的方法-执行[subscriber sendCompleted]->回调[loggingSignal subscribeCompleted:]中的方法

总结:[RACSignal subscribeCompleted:]  [RACSignal subscribeNext:]都会回调createSignl中的block中的方法,但是block中的[sendCompleted] [sendNext]只会执行他回调的方法,即谁回调执行谁。 但是,[doNext]和[doCompleted]中的block会执行。

3.[-do…][RACSignal+Operations] ([doNext:][RACSignal]   [doCompleted:][RACSignal])

__blockunsignedsubscriptions =0;RACSignal *loggingSignal = [RACSignal createSignal:^ RACDisposable * (id subscriber) {    subscriptions++;    [subscriber sendCompleted];returnnil;}];// Does not output anything yetloggingSignal = [loggingSignal doCompleted:^{NSLog(@"about to complete subscription %u", subscriptions);}];// Outputs:// about to complete subscription 1// subscription 1[loggingSignal subscribeCompleted:^{NSLog(@"subscription %u", subscriptions);}];

执行doCompleted或者doNext时,必须返回RACSignal,否则,不会执行其block。[doNext:][doCompleted]不会像[subscribeCompleted:][subscribeNext:]那样主动去回调[createSignal:][RACSignal]中的block,而是,当[subscribe doNext:][subscribe doComplete]时执行相应的动作。

4.[-map:][RACStream]

map:映射的意思,此方法可以将一个信号映射成一个新的任意类型的信号。

RACSequence *letters = [@"A B C D E F G H I"componentsSeparatedByString:@" "].rac_sequence;// Contains: AA BB CC DD EE FF GG HH IIRACSequence *mapped = [letters map:^(NSString *value) {return[valuestringByAppendingString:value];}];

RACSequence *mapped2 = [letters map:(^NSString *value){

return @([value isEqualToString:@“a”]);

}];

如上,可以将原来的letters信号串map成@“AA BB CC DD EE FF GG HH II”信号串,也可以map成BOOL类型的信号串

5.[-filter:][RACStream]

筛选符合条件的信号,组成一个只包含符合条件信号的信号流。

RACSequence *numbers = [@"1 2 3 4 5 6 7 8 9"componentsSeparatedByString:@" "].rac_sequence;// Contains: 2 4 6 8RACSequence *filtered = [numbers filter:^ BOOL (NSString *value) {return(value.intValue %2) ==0;}];

如图,过滤出符合条件的signal;

6.[-concat:][RACStream]

将一个信号流添加到另一个信号流后面。

RACSequence*letters= [@"A B C D E F G H I" componentsSeparatedByString:@""].rac_sequence;RACSequence*numbers= [@"123456789" componentsSeparatedByString:@""].rac_sequence;//Contains: A B C D E F G H I123456789RACSequence*concatenated= [letters concat:numbers];

7.[-flatten][RACStream]

(1)将两个sequence合成一个sequence,其实质就是concatenated。

RACSequence*letters= [@"A B C D E F G H I" componentsSeparatedByString:@""].rac_sequence;RACSequence*numbers= [@"123456789" componentsSeparatedByString:@""].rac_sequence;RACSequence*sequenceOfSequences=@[letters, numbers ].rac_sequence;//Contains: A B C D E F G H I123456789RACSequence*flattened= [sequenceOfSequences flatten];

(2)将两个signal合成一个signal,其实质就是merge。

RACSubject*letters= [RACSubject subject];RACSubject*numbers= [RACSubject subject];RACSignal*signalOfSignals= [RACSignal createSignal:^ RACDisposable * (id subscriber) {    [subscriber sendNext:letters];[subscriber sendNext:numbers];[subscriber sendCompleted];returnnil;}];RACSignal*flattened= [signalOfSignals flatten];//Outputs: A1B C2[flattened subscribeNext:^(NSString*x) {    NSLog(@"%@",x);}];[letters sendNext:@"A"];[numbers sendNext:@"1"];[letters sendNext:@"B"];[letters sendNext:@"C"];[numbers sendNext:@"2"];

调用顺序:createSignal->[flattend subscribeNext:]->[createSignal (^block)]->[letters sendNext:]->[flattend subscribeNext:^block]…(之后重复,sendNext之后,回调subscribeNext);

8.[-flattenMap:][RACStream]

实质:mapping和flattening

RACSequence*numbers= [@"123456789" componentsSeparatedByString:@""].rac_sequence;//Contains:112233445566778899RACSequence*extended= [numbers flattenMap:^(NSString*num) {return@[num, num ].rac_sequence;}];//Contains:1_3_5_7_9_RACSequence*edited= [numbers flattenMap:^(NSString*num) {if(num.intValue %2==0) {return[RACSequence empty];    }else{        NSString*newNum= [num stringByAppendingString:@"_"];

return [RACSequence return:newNum];

}

}];

9.[-then:][RACSignal+Operations]

RACSignal *letters = [@"A B C D E F G H I"componentsSeparatedByString:@" "].rac_sequence.signal;// The new signal only contains:123456789//// But when subscribed to, it also outputs: A B C D E F G H IRACSignal *sequenced =[[letters    doNext:^(NSString *letter) {        NSLog(@"%@", letter);    }]    then:^{return[@"1 2 3 4 5 6 7 8 9"componentsSeparatedByString:@" "].rac_sequence.signal;    }];

sequenced.signal subscribeNext:^(id x){

NSLog(@“%@“, x); //输出1 2 3 4 5 6 7 8,并不会输出A B C D E F G H I

}

执行完doNext:中的方法后,返回一个新的signal给原来的信号,并去执行subscribeNext中的相应方法

10.[+merge:][RACSignal+Operations]

将两个信号流合成一个信号流

RACSubject*letters= [RACSubject subject];RACSubject*numbers= [RACSubject subject];RACSignal*merged= [RACSignal merge:@[letters, numbers ]];//Outputs: A1B C2[merged subscribeNext:^(NSString*x) {    NSLog(@"%@", x);}];[letters sendNext:@"A"];[numbers sendNext:@"1"];[letters sendNext:@"B"];[letters sendNext:@"C"];[numbers sendNext:@"2"];

[-subscribeNext:]相当于水管出水口,当[-sendNext:]有水注入时,水管出口就会有相应的水流出。

11.[+combineLatest:][RACSignal+Operations] and [+combineLatest:reduce:]

观察所有信号的状况,当有信号发生改变时,会将最后改变的信号发送给监听者。

RACSubject*letters= [RACSubject subject];RACSubject*numbers= [RACSubject subject];RACSignal*combined= [RACSignal    combineLatest:@[letters, numbers ]    reduce:^(NSString*letter, NSString*number) {return[letter stringByAppendingString:number];    }];//Outputs: B1 B2 C2 C3[combined subscribeNext:^(idx) {    NSLog(@"%@", x);}];[letters sendNext:@"A"];[letters sendNext:@"B"];[numbers sendNext:@"1"];[numbers sendNext:@"2"];[letters sendNext:@"C"];[numbers sendNext:@"3"];

12.[-switchToLatest][RACSignal+Operations]

用于信号和信号之间,总是选择最新的信号给另一个。

RACSubject*letters= [RACSubject subject];RACSubject*numbers= [RACSubject subject];RACSubject*signalOfSignals= [RACSubject subject];RACSignal*switched= [signalOfSignals switchToLatest];//Outputs: A B1D [switched subscribeNext:^(NSString*x) {     NSLog(@"%@",x);}];[signalOfSignals sendNext:letters];[letters sendNext:@"A"];[letters sendNext:@"B"];[signalOfSignals sendNext:numbers];[letters sendNext:@"C"];[numbers sendNext:@"1"];[signalOfSignals sendNext:letters];[numbers sendNext:@"2"];[letters sendNext:@"D"];

你可能感兴趣的:(RAC学习笔记)