Advanced usage-bind methods common in RAC

Source: Internet
Author: User

RAC Operation idea: Hook (Hook) thought RAC Core method: Bind Bind method False assumptions listen to the contents of the text box, and each time the output results, the text box in the content of splicing a paragraph of text "output:"
    • Mode one: After returning the result, splice.
[_textfield.rac_textsignal subscribenext:^ (ID  x) {NSLog (@ " output:%@"  , x);}];
    • Mode two: Before returning the result, stitching, using the Bind method in RAC to do the processing.
Bind method parameter: Need to pass in a block parameter that returns a value of Racstreambindblock
Racstreambindblock is a block type, the return value is signal, parameter (value,stop), so the block return value of the parameter is also a block.

Racstreambindblock:
Parameter one (value): Indicates that the original value of the received signal has not been processed
Parameter Two (*stop): Used to control the binding block, if *stop = yes, then the binding will end.
Return value: Signal, good processing, in the return through this signal, general use racreturnsignal, need to manually import the header file RACReturnSignal.h.

Bind method Use steps:
1. Pass in a block that returns a value of Racstreambindblock.
2. Describes a racstreambindblock type of Bindblock as the return value of the block.
3. Describe a signal that returns a result, as the return value of Bindblock.
Note: The processing of signal results is done in Bindblock.

Bottom-level implementations:
1. The source signal called bind will re-create a binding signal.
2. When the binding signal is subscribed, the didsubscribe in the binding signal is called, generating a bindingblock.
3. When the source signal has content emitted, it will pass the content to Bindingblock processing, call Bindingblock (Value,stop)
4. Call Bindingblock (value,stop), which returns a signal (racreturnsignal) for the completion of the content processing.
5. Subscribe to Racreturnsignal, you will get the subscriber that binds the signal, send out the signal content that processing completes.

>>> Note: Different Subscribers, save different nextblock, look at the source, it is important to see clearly the Subscriber is which.
You need to manually import #import <reactivecocoa/racreturnsignal.h&gt here to use racreturnsignal.

[[_textfield.rac_textsignal bind:^racstreambindblock{//when to call://Block action: Indicates that a signal is bound.return^racstream * (IDValue, BOOL *stop) {       //When to call block: When the signal has a new value, it will come to the block. //block action: Do the return value processing//do the work and return it through the signal.    return[Racreturnsignalreturn: [NSString stringWithFormat:@"Output:%@", value]];}; Subscribenext:^(IDx) {NSLog (@"%@", x);}];
Mapping (FLATTENMAP,MAP):Used to map the source signal content into a new content map function: Map the value of the source signal to a new value
Map Use steps:
1. Pass in a block, type is the return 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, return directly to the good, do not wrap the signal, the return value, is the value of the map.

Map bottom-level implementations:
The 0.MAP layer is actually called the value returned in the block in the Flatternmap,map as the value in the block in the Flatternmap.
1. When the subscription binds the signal, Bindblock is generated.
3. Bindblock (value, *stop) is called when the source signal sends the content
4. Call Bindblock and the Flattenmap block will be called internally
The block in the map is called inside the 5.flattenMap block to wrap the contents of the block returned in the map as a return signal.
5. The returned signal will eventually be used as the return signal in the Bindblock as the return signal of the bindblock.
6. Subscribe to the Bindblock return signal, you will get the binding signal subscribers, the processing of the completed signal content sent out.
- (void) map{//1. Create a signalRacsubject *subject =[Racsubject subject]; //2. Binding SignalRacsignal *bindsignal = [Subject map:^ID(IDvalue) {        //The returned type is the value you need to map        return[NSString stringWithFormat:@"hmj%@", value];    }]; //3. Subscribe to Signals[Bindsignal subscribenext:^ (IDx) {NSLog (@"%@", x);    }]; //4. Send Data[Subject Sendnext:@"GQ"];}
Flattenmap: Tile map function: For signal signal, the content of the source signal is mapped into a new signal, the signal can be any type flattenmap use steps:
1. Pass in a Block,block type is the return value Racstream, parameter value
2. Parameter value is the content of the source signal, the content of the source signal to do processing
3. Pack the racreturnsignal signal and return it. Flattenmap Bottom implementation: 0.flattenMap internal call bind method implementation, Flattenmap in the return value of the block, will be the return value of Bindblock in bind.
1. When the subscription binds the signal, Bindblock is generated.
2. Bindblock (value, *stop) is called when the source signal sends the content
3. Call Bindblock, the internal will call Flattenmap's Block,flattenmap block role: is to wrap the processed data into a signal.
4. The returned signal will eventually be used as the return signal in the Bindblock as the return signal of the bindblock. 5. Subscribe to the Bindblock return signal, you will receive the binding signal of the subscriber, the processing of the completed signal content sent out
-(void) flattenmap
{    //1. Create a signalRacsubject *subject =[Racsubject subject]; //2. Binding SignalRacsignal *bindsignal = [Subject Flattenmap:^racstream * (IDvalue) {        //Value: Content sent by source signalValue = [NSString stringWithFormat:@"hmj%@", value]; //signal returned: Used to wrap the value of the modified content        return[Racreturnsignalreturn: value];    }]; //what signal is returned in the flattenmap, what signal is subscribed to//Subscribe to Signals[Bindsignal subscribenext:^ (IDx) {NSLog (@"%@", x);    }]; //Send Data[Subject sendnext:@1];}
 The difference between Flatternmap and map * The Block return signal in 1.FLATTERNMAP.
* Block in 2.Map returns the object.
* 3. In development, if the signal emitted by the value is not a signal, the map generally uses map * 4. In development, if the signal emitted by the value is a signal, the mapping is generally used flatternmap. Signalofsignals with Flatternmap
//create a signal in the signalRacsubject *signalofsignals =[Racsubject subject]; Racsubject*signal =[Racsubject subject]; [[Signalofsignals Flattenmap:^racstream * (IDvalue) {       //when Signalofsignals's signals sends a signal, it calls       returnvalue; }] Subscribenext:^(IDx) {//only Signalofsignals's signal is signaled, since the internally subscribed signal returned in Bindblock is the signal returned by Flattenmap. //that is, the signal emitted by Flattenmap is called. NSLog (@"% @aaa", x);}];//Signal signal to send signal[Signalofsignals sendnext:signal];//Signal Sending Content[Signal sendnext:@1];
 

Advanced usage-bind methods common in RAC

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.