iOS-读书笔记

2018-12-20 RAC-Basic Operators

2019-01-06  本文已影响3人  Life淡淡

https://github.com/ReactiveCocoa/ReactiveObjC/blob/master/Documentation/BasicOperators.md

Basic Operators

1:This document explains some of the most common operators used in ReactiveCocoa, and includes examples demonstrating their use.

2:Operators that apply to sequences and signals are known as stream operators.

Typle

1:Performing side effects with signals

    1:Subscription

    2:Injecting effects

2:Transforming streams

    1:Mapping

    2:Filtering

3:Combining streams

    1:Concatenating

    2:Flattening

    3:Mapping and flattening

4:Combining signals

    1:Sequencing

    2:Merging

    3:Combining latest values

    4:Switching

Performing side effects with signals

    1:Most signals start out "cold," which means that they will not do any work until subscription.

    2:Upon subscription, a signal or its subscribers can perform side effects, like logging to the console, making a network request, updating the user interface, etc.

    3:Side effects can also be injected into a signal, where they won't be performed immediately, but will instead take effect with each subscription later

Subscription

    1:The -subscribe… methods give you access to the current and future values in a signal:

Subscription

    2:For a cold signal, side effects will be performed once per subscription

2

    3:This behavior can be changed using a connection.

Injecting effects

1:The -do… methods add side effects to a signal without actually subscribing to it

Injecting effects

Transforming streams

These operators transform a single stream into a new stream.

    1:Mapping

        The -map: method is used to transform the values in a stream, and create a new stream with the results:

map

    2:Filtering

        The -filter: method uses a block to test each value, including it into the resulting stream only if the test passes:

Filtering

Combining streams

These operators combine multiple streams into a single new stream.

    1:Concatenating

        The -concat: method appends one stream's values to another:

    2:Flattening

        The -flatten operator is applied to a stream-of-streams, and combines their values into a single new stream.

    Sequences are concatenated:

concatenated

    Signals are merged:    

merged

Mapping and flattening

1:Flattening isn't that interesting on its own, but understanding how it works is important for -flattenMap:.

2:-flattenMap: is used to transform each of a stream's values into a new stream. Then, all of the streams returned will be flattened down into a single stream. In other words, it's -map: followed by -flatten.

3:This can be used to extend or edit sequences:

1

Or create multiple signals of work which are automatically recombined:

2

Combining signals

These operators combine multiple signals into a single new RACSignal.

Sequencing

-then: starts the original signal, waits for it to complete, and then only forwards the values from a new signal:

1

This is most useful for executing all the side effects of one signal, then starting another, and only returning the second signal's values.

Merging

The +merge: method will forward the values from many signals into a single stream, as soon as those values arrive:

Combining latest values

The +combineLatest: and +combineLatest:reduce: methods will watch multiple signals for changes, and then send the latest values from all of them when a change occurs:

Note that the combined signal will only send its first value when all of the inputs have sent at least one. In the example above, @"A" was never forwarded because numbers had not sent a value yet.

Switching

The -switchToLatest operator is applied to a signal-of-signals, and always forwards the values from the latest signal:

Switching
上一篇 下一篇

猜你喜欢

热点阅读