2018-12-19 RAC-Design Guidelines

https://github.com/ReactiveCocoa/ReactiveObjC/blob/master/Documentation/DesignGuidelines.md#process-only-as-much-of-a-stream-as-needed

Design Guidelines

This document contains guidelines for projects that want to make use of ReactiveCocoa. The content here is heavily inspired by the Rx Design Guidelines.

This document assumes basic familiarity with the features of ReactiveCocoa. The Framework Overview is a better resource for getting up to speed on the functionality provided by RAC.

The RACSequence contract

RACSequence is a pull-driven stream. Sequences behave similarly to built-in collections, but with a few unique twists.

Evaluation occurs lazily by default

1:Sequences are evaluated lazily by default. For example, in this sequence:


1

… no string appending is actually performed until the values of the sequence are needed

2:This generally avoids performing unnecessary work (since values that are never used are never calculated), but means that sequence processing should be limited only to what's actually needed.

3:Once evaluated, the values in a sequence are memoized and do not need to be recalculated. Accessing sequence.headmultiple times will only do the work of one string concatenation.

4:If lazy evaluation is undesirable – for instance, because limiting memory usage is more important than avoiding unnecessary work – the eagerSequence property can be used to force a sequence (and any sequences derived from it afterward) to evaluate eagerly.

Evaluation blocks the caller

1:Regardless of whether a sequence is lazy or eager, evaluation of any part of a sequence will block the calling thread until completed.This is necessary because values must be synchronously retrieved from a sequence.

2:If evaluating a sequence is expensive enough that it might block the thread for a significant amount of time, consider creating a signal with -signalWithScheduler: and using that instead.

Side effects occur only once

1:When the block passed to a sequence operator involves side effects, it is important to realize that those side effects will only occur once per value – namely, when the value is evaluated:


1

The RACSignal contract

1:RACSignal is a push-driven stream with a focus on asynchronous event delivery through subscriptions.

Signal events are serialized

1:A signal may choose to deliver its events on any thread. Consecutive events are even allowed to arrive on different threads or schedulers, unless explicitly delivered onto a particular scheduler.

2:However, RAC guarantees that no two signal events will ever arrive concurrently.

3:While an event is being processed, no other events will be delivered.

4:The senders of any other events will be forced to wait until the current event has been handled.

5:Most notably, this means that the blocks passed to -subscribeNext:error:completed: do not need to be synchronized with respect to each other, because they will never be invoked simultaneously.

Subscription will always occur on a scheduler

1:To ensure consistent behavior for the +createSignal: and -subscribe: methods, each RACSignal subscription is guaranteed to take place on a valid RACScheduler.

2:If the subscriber's thread already has a +currentScheduler, scheduling takes place immediately; otherwise, scheduling occurs as soon as possible on a background scheduler.

3:Note that the main thread is always associated with the +mainThreadScheduler, so subscription will always be immediate there.

Errors are propagated immediately

1:In RAC, error events have exception semantics. When an error is sent on a signal, it will be immediately forwarded to all dependent signals, causing the entire chain to terminate.

2:Operators whose primary purpose is to change error-handling behavior – like -catch:, -catchTo:, or -materialize – are obviously not subject to this rule.

Side effects occur for each subscription

1:Each new subscription to a RACSignal will trigger its side effects. 

2: This means that any side effects will happen as many times as subscriptions to the signal itself.


3:Side effects are repeated for each subscription. The same applies to stream and signal operators:


2

4:To suppress this behavior and have multiple subscriptions to a signal execute its side effects only once, a signal can bemulticasted.


1

5:Side effects can be insidious and produce problems that are difficult to diagnose. For this reason it is suggested to make side effects explicit when possible.

2

Subscriptions are automatically disposed upon completion or error

1:When a subscriber is sent a completed or error event, the associated subscription will automatically be disposed. This behavior usually eliminates the need to manually dispose of subscriptions.

2:See the Memory Management document for more information about signal lifetime.

Disposal cancels in-progress work and cleans up resources

1:When a subscription is disposed, manually or automatically, any in-progress or outstanding work associated with that subscription is gracefully cancelled as soon as possible, and any resources associated with the subscription are cleaned up.

2:Disposing of the subscription to a signal representing a file upload, for example, would cancel any in-flight network request, and free the file data from memory.

©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 211,376评论 6 491
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 90,126评论 2 385
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 156,966评论 0 347
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 56,432评论 1 283
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 65,519评论 6 385
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 49,792评论 1 290
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 38,933评论 3 406
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 37,701评论 0 266
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 44,143评论 1 303
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 36,488评论 2 327
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 38,626评论 1 340
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 34,292评论 4 329
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 39,896评论 3 313
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 30,742评论 0 21
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 31,977评论 1 265
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 46,324评论 2 360
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 43,494评论 2 348

推荐阅读更多精彩内容

  • rljs by sennchi Timeline of History Part One The Cognitiv...
    sennchi阅读 7,309评论 0 10
  • 夕阳一醉落西坡, 绿柳扶风映清波。 人稀林密芳草远, 烟凉苍岭秋雁过。
    时光清浅阿莲阅读 227评论 0 5
  • 【日精进打卡738天】 姓名:孙玉生 沈阳盛和商学院 六项精进第177期学员(队长),271期284期288期29...
    _玉_生_阅读 469评论 0 3
  • 让一步天地宽 林则徐有一句名言:“海纳百川,有容乃大。”与人相处,有一份退让,就受一分益;吃一分亏,就积一次福。相...
    世界旅馆阅读 848评论 0 0
  • 【付海涛谈农牧电商】 互联网是魔幻的,越来越多的信息让注意力分散,却又在大数据算法和一个接一个15秒的短视频下魔幻...
    农牧学堂一付海涛阅读 204评论 0 0