码迷,mamicode.com
首页 > 其他好文 > 详细

RAC-框架概览

时间:2015-10-13 15:08:43      阅读:265      评论:0      收藏:0      [点我收藏+]

标签:

流(RACStream):一个流表示多个对象值组成的序列,对象值可以是当前已有的,也可以是未来会出现的,但是必须先后按顺序依次被检索。

信号(RACSignal):信号是推送型的流,信号可以通过订阅来传递事件给订阅者,主要有三类事件:next,error,complete。next可以携带一个正常的值对象,error表示发生异常并传递NSError对象,completed表示成功结束。信号的生命周期中可以发出多个next事件,然后跟着一个error事件或一个complete事件。

订阅者(RACSubscriber):当对信号使用subscribeNext:error:completed等订阅接口时,RAC会隐式的生成一个订阅者。该订阅者持有该信号,直到信号发生了error或complete事件,订阅者会被自动清理掉。

主题(RACSubject): 主题是一种灵活的信号,可以被随时用来发送next事件,不用考虑是否有订阅者存在

Commands

command, represented by the RACCommand class, creates and subscribes to a signal in response to some action. This makes it easy to perform side-effecting work as the user interacts with the app.

Usually the action triggering a command is UI-driven, like when a button is clicked. Commands can also be automatically disabled based on a signal, and this disabled state can be represented in a UI by disabling any controls associated with the command.

On OS X, RAC adds a rac_command property to NSButton for setting up these behaviors automatically.

Connections

connection, represented by the RACMulticastConnection class, is a subscription that is shared between any number of subscribers.

Signals are cold by default, meaning that they start doing work each time a new subscription is added. This behavior is usually desirable, because it means that data will be freshly recalculated for each subscriber, but it can be problematic if the signal has side effects or the work is expensive (for example, sending a network request).

A connection is created through the -publish or -multicast: methods on RACSignal, and ensures that only one underlying subscription is created, no matter how many times the connection is subscribed to. Once connected, the connection‘s signal is said to be hot, and the underlying subscription will remain active until all subscriptions to the connection are disposed.

Sequences

sequence, represented by the RACSequence class, is a pull-driven stream.

Sequences are a kind of collection, similar in purpose to NSArray. Unlike an array, the values in a sequence are evaluated lazily (i.e., only when they are needed) by default, potentially improving performance if only part of a sequence is used. Just like Cocoa collections, sequences cannot contain nil.

Sequences are similar to Clojure‘s sequences (lazy-seq in particular), or the List type in Haskell.

RAC adds a -rac_sequence method to most of Cocoa‘s collection classes, allowing them to be used as RACSequences instead.

Disposables

The RACDisposable class is used for cancellation and resource cleanup.

Disposables are most commonly used to unsubscribe from a signal. When a subscription is disposed, the corresponding subscriber will not receive any further events from the signal. Additionally, any work associated with the subscription (background processing, network requests, etc.) will be cancelled, since the results are no longer needed.

For more information about cancellation, see the RAC Design Guidelines.

Schedulers

scheduler, represented by the RACScheduler class, is a serial execution queue for signals to perform work or deliver their results upon.

Schedulers are similar to Grand Central Dispatch queues, but schedulers support cancellation (via disposables), and always execute serially. With the exception of the +immediateScheduler, schedulers do not offer synchronous execution. This helps avoid deadlocks, and encourages the use of signal operators instead of blocking work.

RACScheduler is also somewhat similar to NSOperationQueue, but schedulers do not allow tasks to be reordered or depend on one another.

Value types

RAC offers a few miscellaneous classes for conveniently representing values in a stream:

  • RACTuple is a small, constant-sized collection that can contain nil (represented by RACTupleNil). It is generally used to represent the combined values of multiple streams.
  • RACUnit is a singleton "empty" value. It is used as a value in a stream for those times when more meaningful data doesn‘t exist.
  • RACEvent represents any signal event as a single value. It is primarily used by the -materialize method ofRACSignal.

RAC-框架概览

标签:

原文地址:http://www.cnblogs.com/guoxiaoqian/p/4874521.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!