Reactivecocoa-publish、multicast、replay、replayLast

//-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
  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);
    }];
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);
    }];
   
    [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] second next value = signal1
signal1只是被执行了一次,神奇不? 详见 http://www.jianshu.com/p/a0a821a2480f

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值