iamyogish
iamyogish

Reputation: 2432

ReactiveCocoa - concat flatten strategy not working as expected

I've started to learn reactive-cocoa from couple of days, today I was playing with the flatten method of the reactivecocoa (reactiveSwift), I tried executing the snippet given for the concat flattening in the documentation Basic operators. Here's the snippet:

let (lettersSignal, lettersObserver) = Signal<String, NoError>.pipe()
let (numbersSignal, numbersObserver) = Signal<String, NoError>.pipe()
let (signal, observer) = Signal<Signal<String, NoError>, NoError>.pipe()

signal.flatten(.concat).observeValues { print($0) }

observer.send(value: lettersSignal)
observer.send(value: numbersSignal)
observer.sendCompleted()

numbersObserver.send(value: "1")    // nothing printed
lettersObserver.send(value: "a")    // prints "a"
lettersObserver.send(value: "b")    // prints "b"
numbersObserver.send(value: "2")    // nothing printed
lettersObserver.send(value: "c")    // prints "c"
lettersObserver.sendCompleted()     // prints "1, 2"
numbersObserver.send(value: "3")    // prints "3"
numbersObserver.sendCompleted()

As per the documentation and the interactive visualization diagram (RAC marbles - flatten(.concat) visual diagram, the output should have been something like this,

First it should have printed letter stream i.e,

a, b, c

& once the letterStream has completed it should've printed the number stream i.e.

1, 2, 3

So the final output of this observation should've been

[a, b, c, 1, 2, 3]

However, the concatenated output I'm seeing is,

[a, b, c, 3]

why is this so? Why only the latest value of the numberStream is being printed? Instead of printing the entire number stream values once the letter stream was completed.

Please let me know if I've misunderstood something. Cheers.

Upvotes: 1

Views: 477

Answers (1)

Rui Peres
Rui Peres

Reputation: 25917

As mentioned in the ReactiveSwift's slack channel, that is the expected outcome.

Quoting the documentation:

The outer event stream is started observed. Each subsequent event stream is not observed until the preceeding one has completed.

So numbersSignal will only send values, once lettersObserver has completed.

Upvotes: 4

Related Questions