//-replay 总是收取最后的内容,而并不执行signal
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | __block int num = 0; RACSignal *signal = [[RACSignal createSignal:^RACDisposable *(id subscriber) { num++; NSLog(@"Increment num to: %i", num); [subscriber sendNext:@(num)]; return nil; }] replay]; NSLog(@"Start subscriptions"); // Subscriber 1 (S1) [signal subscribeNext:^(id x) { NSLog(@"S1: %@", x); }]; // Subscriber 2 (S2) [signal subscribeNext:^(id x) { NSLog(@"S2: %@", x); }]; // Subscriber 3 (S3) [signal subscribeNext:^(id x) { NSLog(@"S3: %@", x); }]; |
Increment num to: 1
Start subscriptions
S1: 1
S2: 1
Start subscriptions
S1: 1
S2: 1
S3: 1
-replay 总是取出第一订阅者取到的
所有结果
-replayLast 总是取出第一个订阅者取到的
最后一个结果
-replayLazily有点说不上来
replayLazily does not subscribe to the signal immediately – it lazily waits until there is a “real” subscriber. But replay subscribes immediately. So as you point out, with replayLazily the “A” value would not be sent to subscribers of the signal because it was sent before there was anything listening.
RACMulticastConnection public connect
当一个信号被订阅了几次,那么它将会执行几次,见下方代码:
RACSignal
*signal1 = [
RACSignal
defer
:^
RACSignal
*{
NSLog ( @"print signal1" );
return [ RACSignal return : @"hello" ];
}];
[signal1 subscribeNext :^( id x) {
NSLog ( @"first %@" ,x);
}];
[signal1 subscribeNext :^( id x) {
NSLog ( @"second %@" ,x);
NSLog ( @"print signal1" );
return [ RACSignal return : @"hello" ];
}];
[signal1 subscribeNext :^( id x) {
NSLog ( @"first %@" ,x);
}];
[signal1 subscribeNext :^( id x) {
NSLog ( @"second %@" ,x);
}];
2015-06-05 14:35:38.149 DemoCategorizer[15252:2226982] print signal1
2015-06-05 14:35:38.149 DemoCategorizer[15252:2226982] first hello
2015-06-05 14:35:38.149 DemoCategorizer[15252:2226982] print signal1
2015-06-05 14:35:38.149 DemoCategorizer[15252:2226982] first hello
2015-06-05 14:35:38.149 DemoCategorizer[15252:2226982] print signal1
2015-06-05 14:35:38.149 DemoCategorizer[15252:2226982] second hello
那么,我打算某个网络操作只做一次,然后多个订阅者都可以收到消息,怎么做?
RACSignal
*signal1 = [
RACSignal
defer
:^
RACSignal
*{
NSLog ( @"print signal1" );
return [ RACSignal return : @"signal1" ];
}];
RACMulticastConnection *connection = [signal1 publish ];
[connection. signal subscribeNext :^( id x) {
NSLog ( @"first next value = %@" ,x);
}];
[connection. signal subscribeNext :^( id x) {
NSLog ( @"second next value = %@" ,x);
}];
NSLog ( @"print signal1" );
return [ RACSignal return : @"signal1" ];
}];
RACMulticastConnection *connection = [signal1 publish ];
[connection. signal subscribeNext :^( id x) {
NSLog ( @"first next value = %@" ,x);
}];
[connection. signal subscribeNext :^( id x) {
NSLog ( @"second next value = %@" ,x);
}];
[connection connect];
2015-06-05 14:38:48.528 DemoCategorizer[15848:2239991] print signal1
2015-06-05 14:38:48.528 DemoCategorizer[15848:2239991] first next value = signal1
2015-06-05 14:38:48.528 DemoCategorizer[15848:2239991] first next value = signal1
2015-06-05 14:38:48.528 DemoCategorizer[15848:2239991] second next value = signal1
signal1只是被执行了一次,神奇不? 详见
http://www.jianshu.com/p/a0a821a2480f