//-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) "S2: %@",10)">// Subscriber 3 (S3) "S3: %@",0)">]; |
Incrementnumto:1
Startsubscriptions
S1:1
S2:1
Startsubscriptions
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
:^
*{
NSLog ( @"print signal1" );
return [ RACSignal : @"hello" ];
}];
[signal1 subscribeNext :^( id x) {
@"first %@" ,x);
}];
[signal1 @"second %@"
NSLog ( @"print signal1" );
return [ RACSignal : @"hello" ];
}];
[signal1 subscribeNext :^( id x) {
@"first %@" ,x);
}];
[signal1 @"second %@"
}];
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
那么,我打算某个网络操作只做一次,然后多个订阅者都可以收到消息,怎么做?
@"signal1"
];
}];
RACMulticastConnection *connection = [signal1 publish ];
[connection. signal @"first next value = %@" @"second next value = %@"
}];
RACMulticastConnection *connection = [signal1 publish ];
[connection. signal @"first next value = %@" @"second next value = %@"
[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