Reactivecocoa is a very complex framework, and before we begin to introduce its core components, let's look at its class diagram to get a macro view of its hierarchy:
From the above class diagram, we can see that Reactivecocoa is mainly composed of the following four core components: signal Source: Racstream and its subclasses; Subscriber: Racsubscriber implementation class and its subclasses; Scheduler: Racscheduler and its subclasses; Cleaners: Racdisposable and their subclasses.
Among them, the signal source is the core part, the other components are operating around it.
the difference between racsignal and Racsubject racsubject: Signal providers, they can act as a signal, but also to send a signal to use the scene: usually used to replace the agent , with it, you do not have to define the agent
Racsubject Use steps
1. Create signal [racsubject subject], unlike RACSIGANL, there is no block when creating a signal.
2. Subscription signal-(racdisposable *) Subscribenext: (void (^) (id x)) nextblock
3. Send signal Sendnext: (ID) value
Racsubject: The bottom implementation is not the same as racsignal.
1. Call the Subscribenext subscription signal, just save the Subscriber, and the subscriber's Nextblock has been assigned value.
2. Call Sendnext to send a signal, traverse all just saved subscribers, one call subscriber's nextblock.
Racsubject instance after the map operation, send finished must call-sendcompleted, otherwise there will be memory leaks; The racsignal instance does not have a memory leak, regardless of whether the map operation is performed or not, regardless of whether the-sendcompleted is invoked.
Reason: Because Racsubject is a hot signal, in order to ensure future events occur, subscribers can receive information, so need to hold subscribers!
the difference between map and Flattenmap Map applies To: a signal emits a value
Flatternmap applies To: signals are emitted by signals
Let's take a look at the RACStream.h document description:
Maps ' block ' across the values in the receiver.
///
This is corresponds to the ' Select ' method inRx. (Rx see note below)
///
Returns a new stream with the mapped values.
-(Instancetype) Map: (ID (^) (id value)) block;
Maps ' block ' across the values in the receiver and flattens.
///
Note This operators applied _after_-flattenmap:behave differently from
Operators _within_-flattenmap:. The examples section below.
///
This is corresponds to the ' SelectMany ' method in Rx.
///
BLOCK-A block which accepts the values in the receiver and returns A new
Instance of the receiver ' s class. Returning ' nil ' from the ' is '
Equivalent to returning a empty signal.
///
Examples
///
[Signal flattenmap:^ (ID x) {
Logs each time a returned signal completes.
return [[Racsignal return:x] logcompleted];
/// }];
///
[[Signal
flattenmap:^ (ID x) {
return [racsignal return:x];
/// }]
Logs only once the signals complete.
Logcompleted];
///
Returns a new stream which represents the combined streams resulting from
Mapping ' block '.
-(Instancetype) Flattenmap: (Racstream * (^) (id value)) block;
what is Rx: "Reactive Extensions (RX) is a Microsoft Class library that integrates asynchronous, observable (observable) sequence based event-driven programming and Linq-style query operations. With RX, developers can use the observable object to describe asynchronous data streams, query data asynchronously using LINQ operators, and use schedulers to control concurrency in asynchronous processes. In short, Rx = observables + LINQ + schedulers. ”
- (void) map { // map Use steps : // 1. Pass in a block, type is returned object, parameter is value / / 2.value is the content of the source signal, directly to the content of the source signal to do processing // 3. Take the processed content and return it directly, without packing the signal, return the value , which is the value of the map. // map Bottom implementation: The bottom of the // 0.map is actually calling the value returned in the block in Flatternmap,map as the value in the block in Flatternmap. // 1. When a subscription binds a signal, a bindblock is generated. // 3. When the source signal is sent, the Bindblock (value, *stop) is invoked. // 4. Call Bindblock, the Flattenmap block // 5 is called internally. Within the Flattenmap block, the block in the map is invoked to wrap the returned contents of the block in the map to the returned signal. // 5. The returned signal will eventually act as a return signal in Bindblock as a bindblock return signal. &Nbsp; // 6. Subscribe to the Bindblock return signal, you will get the subscriber of the binding signal, the processing of the completed signal content sent out. // map Function: Map the value of the source signal to a new value // Create Signal RACSubject *subject = [RACSubject subject]; // binding signal RACSignal *bindSignal = [subject map:^id (id value) { // The return type is the value you need to map return [nsstring stringwithformat:@ "ws:%@", value]; // Here the source signal sent the "123" Front stitching ws: }]; // Subscription binding signal [bindsignal subscribenext:^ (id x) { nslog ( @ "%@", x); }]; // Send signal