转:RAC中比较replay, replayLast, and replayLazily
A co-worker recently asked me about the difference between -replay, -replayLast, and -replayLazily in the ReactiveCocoa library. I had a vague understanding of the three but was not able to confidently explain the difference, so I thought I would look into it further.
I found the header documentation to be difficult to understand if you don’t have a good understanding of RACReplaySubject and RACMulticastConnection, so I’m going to try to explain the replay methods without getting into those underlying concepts.
Subscribing to a Signal
With a “normal” RACSignal each subscription to the signal causes the subscription code to be executed again, and the subscriber only receives values that are sent after the subscription is made. I think it is easiest to show this in two different examples.
The first example shows how the subscription code gets re-executed on each subscription.
1 |
__block int num = 0; |
Running this example will produce:
1 |
Start subscriptions |

As you can see, each subscription causes num to be incremented. Also note that num is not incremented until a subscription is made. In this way, a normal RACSignal can be thought of as lazy, as it doesn’t do any work until it has a subscriber.
Our second example shows how each subscriber only receives the values that are sent after their subscription is added.
1 |
RACSubject *letters = [RACSubject subject]; |
1 |
Subscribe S1 |

In many cases this is the desired behavior. But in some cases, you don’t want the subscription code to be re-executed, e.g. a subscription makes a request to a web service and there are multiple listeners that need to be updated when the result comes in. Or maybe you want to get the history of values that have been sent on the signal prior to a subscription. This is where -replay, -replayLast, and -replayLazily can be used.
Subscribing to a -replay Signal
The -replay convenience method returns a new signal that, when subscribed to, will immediately send the subscriber the entire history of values that have come through the source signal, without re-executing the source signal’s subscription code. The subscriber will still receive all future values from the signal just as it would from a normal signal.
The first example shows how the subscription code is not re-executed upon new subscriptions:
1 |
__block int num = 0; |
1 |
Increment num to: 1 |

This time the num integer is incremented immediately, before there are even any subscribers. And it is only incremented once, meaning that the subscription code is only been executed a single time, regardless of how many subscribers the signal has.
The second example shows how each new subscriber receives the full history of the signal:
1 |
RACSubject *letters = [RACSubject subject]; |
1 |
Subscribe S1 |

Even though S3 subscribed after all of the values had been sent, it still received all of the values.
Subscribing to a -replayLast Signal
The -replayLast convenience method returns a new signal that, when subscribed to, will immediately send the subscriber the most recent value that comes through the source signal, without re-executing the source signal’s subscription code. The subscriber will then receive all future values from the signal just as it would from a normal signal.
For the first example, there is no difference between -replayLast and -replay so I won’t bother showing it again.
The second example illustrates how only the most recent value is provided to a new subscriber.
1 |
RACSubject *letters = [RACSubject subject]; |
1 |
Subscribe S1 |

Subscribing to a -replayLazily Signal
The -replayLazily convenience method returns a new signal that, when subscribed to, will immediately send the subscriber the entire history of values that have come through the source signal, without re-executing the source signal’s subscription code. The difference between -replayLazily and -replay is that -replayLazily will not subscribe to the source signal until something subscribes to the newly created signal. This is opposed to the behavior of -replay and -replayLast, which subscribe to the source signal immediately upon being called.
The first example illustrates this difference. Note how the “Increment num to: 1” message does not appear until after the first subscription. With -replay (and -replayLast) the same message appears before the first subscription.
1 |
__block int num = 0; |
1 |
Start subscriptions |

And the second example shows that the full history is sent to any new subscribers, just like with -replay.
1 |
RACSubject *letters = [RACSubject subject]; |
1 |
Subscribe S1 |

Summary
ReactiveCocoa provides three convenience methods for allowing multiple subscribers to the same signal, without re-executing the source signal’s subscription code, and to provide some level of historical values to later subscribers. -replay and -replayLast both make the signal hot, and will provide either all values (-replay) or the most recent (-replayLast) value to subscribers. -replayLazily returns a cold signal that will provide all of the signal’s values to subscribers.
转:RAC中比较replay, replayLast, and replayLazily的更多相关文章
- ReactiveCocoa比较区分replay, replayLast和replayLazily
一直搞不清楚replayLazily和replay的区别可以直接跳到最后看. 原文:http://spin.atomicobject.com/2014/06/29/replay-replaylast- ...
- Oracle 10g RAC中的DRM问题及关闭
在RAC环境中,Oracle使用GRD(Global Resource Service)来记录各个RAC节点的资源信息,具体通过GCS(Global Cache Service)和GES(Global ...
- rac中 kull session会话脚本
方法:ALTER SYSTEM KILL SESSION '80, 6, @2'; --<= 80 sid,6 serial#,@2 inst_id kill session 脚本如下:sel ...
- 这里的*号实际表示就是RAC中所有实例都使用
您的位置: ITPUB个人空间 » cc59的个人空间 » 日志 发布新日志 我的日志我的足迹我的收藏 unix/linuxHA随笔backup&restoreperformance tuni ...
- 使用OpenFiler来模拟存储配置RAC中ASM共享盘及多路径(multipath)的测试
第一章 本篇总览 之前发布了一篇<Oracle_lhr_RAC 12cR1安装>,但是其中的存储并没有使用多路径,而是使用了VMware自身提供的存储.所以,年前最后一件事就是把多路径学习 ...
- 详解 RAC 中各种IP和监听的意义
一.SCAN 概念 SCAN(Single Client Access Name)是 Oracle从11g R2开始推出的,客户端可以通过 SCAN 特性负载均衡地连接到 RAC数据库 SCAN 最明 ...
- 关于Oracle RAC中SCN原理和机制的探索
今天看书时看到了关于RAC中SCN的问题,为了进一步搞清楚其内部原理和机制,对该问题进行了广泛的查阅和搜索,遗憾的是,可以参考的资料很少,网上大部分是人云亦云的帖子,其中,详细介绍其内部原理和机制的资 ...
- Oracle 11G R2 RAC中的scan ip 的用途和基本原理【转】
Oracle 11G R2 RAC增加了scan ip功能,在11.2之前,client链接数据库的时候要用vip,假如你的cluster有4个节点,那么客户端的tnsnames.ora中就对应有四个 ...
- 转 rac中并行 PARALLEL 的设置
sample 1: rac中并 行的设置 https://blog.csdn.net/wll_1017/article/details/8285574 我们的生产库一般在节点一上的压力比较大,在节点二 ...
随机推荐
- 第二阶段Sprint冲刺会议8
进展:重新规划主界面,把视频录制暂放到主页面里,先实现功能,视频提醒后期再做.
- 炸弹人 之 N A B C D
团队开发之个人——NABCD理解 项目名称:炸弹人(app)N(need): 随着移动终端的发展,各类软件的需求必然会有长期的需求,而游戏类软件是不同年龄阶段的人共同的需求,我们将要开发的这款游 ...
- 信安实践——CSRF攻击与防御
1.实验原理 CSRF(Cross-Site Request Forgery,跨站点伪造请求)是一种网络攻击方式,该攻击可以在受害者毫不知情的情况下以受害者名义伪造请求发送给受攻击站点,从而在未授权的 ...
- SqlHelper类的编写
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.D ...
- Scrum 6.0
sprint演示 1.坚持所有的sprint都结束于演示. 团队的成果得到认可,会感觉很好. 其他人可以了解你的团队在做些什么,并得到重要反馈. 演示是一种社会活动,不同的团队可以在这里相互交流,讨论 ...
- JMeter性能测试基础 (3) - 使用参数文件做搜索引擎性能对比
本篇文章主要对如何在JMeter中进行URL的参数进行配置进行介绍,通过CSV文件配置参数数据,对baidu.sogou.haosou进行搜索性能对比测试. 1.建立测试计划.线程组,并在线程组下添加 ...
- Java如何查看死锁?
转载自 https://blog.csdn.net/u014039577/article/details/52351626 Java中当我们的开发涉及到多线程的时候,这个时候就很容易遇到死锁问题,刚开 ...
- 网页正文提取,降噪的实现(readability/Document)
安装: pip install readability-lxml 使用: # encoding:utf-8import html2textimport requestsimport refrom re ...
- js全端
js是世界上最好的语言之一,或许可以不用加之一 我是个js游戏前端开发者,但是我不局限于只是开发h5游戏,微信小游戏... js很强大很强大很强大,没有哪种语言能通吃,除了js. 网页, app, 服 ...
- ORA-01034和ORA-27101的解决方法
问题所在: 1.要登录的数据库实例内容配置内容错误,联系负责该机子的管理员看原因