2018-12-20 RAC-Basic Operators
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:Mapping
2:Merging
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:
![](https://img.haomeiwen.com/i1490832/25fd88bcd6a4d8cf.png)
2:For a cold signal, side effects will be performed once per subscription
![](https://img.haomeiwen.com/i1490832/e920103fe7e6b70b.png)
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
![](https://img.haomeiwen.com/i1490832/e8cbbce1525ce5b9.png)
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:
![](https://img.haomeiwen.com/i1490832/d8cf994823935ef9.png)
2:Filtering
The -filter: method uses a block to test each value, including it into the resulting stream only if the test passes:
![](https://img.haomeiwen.com/i1490832/b542696cda05f461.png)
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:
![](https://img.haomeiwen.com/i1490832/1128b4f0e9ed1612.png)
Signals are merged:
![](https://img.haomeiwen.com/i1490832/74a587b8d0bcf4f2.png)
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:
![](https://img.haomeiwen.com/i1490832/35ff251d2d9fcab9.png)
Or create multiple signals of work which are automatically recombined:
![](https://img.haomeiwen.com/i1490832/16234ce9a465cf12.png)
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:
![](https://img.haomeiwen.com/i1490832/4301f64aa9f00465.png)
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:
![](https://img.haomeiwen.com/i1490832/e92eaeae24bae812.png)
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:
![](https://img.haomeiwen.com/i1490832/a2899d8c4a7a5241.png)
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:
![](https://img.haomeiwen.com/i1490832/2744c3bd4f9a799a.png)