简体   繁体   中英

Are there any ways to make several signals into one signal instead of flatten them in Reactive Cocoa

First. Let me introduce the background of this question. I have several network requests to send.

(RA means Request A, and -----> means the result of the previous request will be sent to the next request)

  • RA---->RB---->RC
  • RD
  • RE---->RF

Here are 3 bundles of requests. Now I want to subscribe the signal that any of the bundles that has the response.

Since every request can be represented as a signal eg SA, so the signal flow should look like:

  • SA---->SB----->SC
  • SD
  • SE---->SF

Then are there any signals in the Reactive Cocoa to handle this?

What I do now is to flatten map them

Signal* bundleOne = [[[SA flatternMap:(^RACStream *(NSString* resultOfA) {
                         return SB;
                    }] flatternMap:(^RACStream *(NSString* resultOfB) {
                         return SC;
                    ];

 Signal* bundleTwo = SD;

 Signal* bundleThree = [[[SE flatternMap:(^RACStream *(NSString* resultOfE) {
                         return SF;
                       }]; 

The next step would be that how to create a signal when any of the bundleOne, bundleTwo, bundleThree signals has result.

I have tried

[[RACSignal merge: @[bundleOne, bundleTwo, bundleThree]] any] subscribeNext:
          ^(id object) {xxx}];

But failed. Because the bundle signal is made up of the flattened signals, and from the implementation of the any , it also flattens the signal. So that SA, SB, SC, SD, SE, SF are all flattened and when SA returns, the any signal returns. This is not what I want. (I want that SA--->SB--->SC, the final SC returns, the bundleOne returns)

Could any one help me? or Reactive Cocoa doesn't fit for this? I have to resort to the nested block again....

Thanks a lot.

Try the zip: operator. I am not sure if I completely understand what you want to do but I think zip might do the trick.

see the zip docs

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM