Analysis of Reactivecocoa source code splitting (II.)

Source: Internet
Author: User

The above cobwebs the most important signal mechanism to leave the division. But the various operations in RAC are also essential, and some complex operators are also a combination of basic operations, a bit of the flavor of building blocks.

Then I follow the previous thinking, write some simple operators how to achieve.

Operation appended Concat (splicing)

Concat Use Example

Qhqsignal *demooriginsignal = [qhqsignal createsignal:^ (ID subscriber) {

[Subscriber sendnext:@ "Demooriginsignal-send"];

[Subscriber sendcompleted];

}];

Qhqsignal *democoncatsignal = [qhqsignal createsignal:^ (ID subscriber) {

[Subscriber sendnext:@ "Democoncatsignal-send"];

}];

[[Demooriginsignal concat:democoncatsignal] subscribenext:^ (ID x) {

NSLog (@ "%@", X);

}];

Output content

2015-12-25 10:02:45.966 pagetext[88233:4685875] Demooriginsignal-send

2015-12-25 10:02:45.967 pagetext[88233:4685875] Democoncatsignal-send

achieved the effect of stitching

The realization of the idea is very simple, that is, when the first signal sendcompleted, let subscirber subscribe to the signal splicing.

-(Qhqsignal *) Concat: (qhqsignal *) concatsignal {

return [qhqsignal createsignal:^ (ID subscriber) {

[Self subscribenext:^ (id next) {

[Subscriber Sendnext:next];

} error:^ (Nserror *error) {

[Subscriber Senderror:error];

} completed:^{

[Concatsignal Subscribe:subscriber];

}];

}];

}

It can be seen that when the self (source signal) sends next, the new signal also sends next, sending the error, and the new signal also sends the error. When sending a completed, subscribe to subscribe to the signal to be spliced, so that the stitching is realized.

To implement a more complex operator zip,zip is to compress 2 signals. Whenever 2 signals have a new value, the two new values are returned in the tuple situation, and the demo is returned as an array. (tuples can be found in swift, where RAC implements tuples with macros).

Qhqsignal *demooriginsignal = [qhqsignal createsignal:^ (ID subscriber) {

[Subscriber sendnext:@ "Demooriginsignal-send-zip1"];

[Subscriber sendnext:@ "DEMOORIGINSIGNAL-SEND-ZIP2"];

}];

Qhqsignal *democoncatsignal = [qhqsignal createsignal:^ (ID subscriber) {

[Subscriber sendnext:@ "Democoncatsignal-send-zip1"];

[Subscriber sendnext:@ "DEMOCONCATSIGNAL-SEND-ZIP2"];

}];

[[Demooriginsignal zip:democoncatsignal] subscribenext:^ (ID x) {

NSLog (@ "%@", X);

}];

Output results

2015-12-25 11:10:44.546 pagetext[89544:4713950] (

"Demooriginsignal-send-zip1",

"Democoncatsignal-send-zip1"

)

2015-12-25 11:10:44.547 pagetext[89544:4713950] (

"Demooriginsignal-send-zip2",

"Democoncatsignal-send-zip2"

)


After commenting out any of the send, the output will be reduced once, and if a signal does not send the content, there will be no output.

-(Qhqsignal *) Zip: (qhqsignal *) Signal {

return [qhqsignal createsignal:^ (ID subscriber) {

Nsmutablearray *selfarray = [Nsmutablearray array];

Nsmutablearray *ziparray = [Nsmutablearray array];

void (^sendzip) (void) = ^{

if (Selfarray.count = = 0) return;

if (Ziparray.count = = 0) return;

Nsarray *send = [Nsarray arrayWithObjects:selfArray.firstObject, Ziparray.firstobject,nil];;

[Selfarray removeobjectatindex:0];

[Ziparray removeobjectatindex:0];

[Subscriber Sendnext:send];

};

[Self subscribenext:^ (ID x) {

[Selfarray addobject:x];

Sendzip ();

}];

[Signal subscribenext:^ (ID x) {

[Ziparray addobject:x];

Sendzip ();

}];

}];

}

SOURCE display: The compressed signal with two arrays represents the value of the pre-compression signal. The signal is not packaged out until it has a value. is a relatively simple implementation.

The rest of the operation can be based on the use of the source code analysis, understand the principle, with more confidence.

Analysis of Reactivecocoa source code splitting (II.)

Related Article

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.