ReactiveCocoa - concat 展平策略未按预期工作
ReactiveCocoa - concat flatten strategy not working as expected
我这几天开始学习reactive-cocoa,今天我在玩reactivecocoa(reactiveSwift)的flatten方法,我尝试执行给定的代码片段文档 Basic operators 中的 concat 扁平化。这是片段:
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([=10=]) }
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()
根据文档和交互式可视化图表(RAC marbles - flatten(.concat) visual diagram,输出应该是这样的,
首先它应该打印字母流,即
a, b, c
& 一旦 letterStream 完成,它应该打印数字流,即
1, 2, 3
所以这个观察的最终输出应该是
[a, b, c, 1, 2, 3]
但是,我看到的串联输出是,
[a, b, c, 3]
为什么会这样?为什么只打印 numberStream 的最新值?而不是在字母流完成后打印整个数字流值。
如果我误解了什么,请告诉我。干杯。
如ReactiveSwift's slack channel中所述,这是预期的结果。
The outer event stream is started observed. Each subsequent event stream is not observed until the preceeding one has completed.
所以 numbersSignal
只会在 lettersObserver
完成后发送值。
我这几天开始学习reactive-cocoa,今天我在玩reactivecocoa(reactiveSwift)的flatten方法,我尝试执行给定的代码片段文档 Basic operators 中的 concat 扁平化。这是片段:
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([=10=]) }
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()
根据文档和交互式可视化图表(RAC marbles - flatten(.concat) visual diagram,输出应该是这样的,
首先它应该打印字母流,即
a, b, c
& 一旦 letterStream 完成,它应该打印数字流,即
1, 2, 3
所以这个观察的最终输出应该是
[a, b, c, 1, 2, 3]
但是,我看到的串联输出是,
[a, b, c, 3]
为什么会这样?为什么只打印 numberStream 的最新值?而不是在字母流完成后打印整个数字流值。
如果我误解了什么,请告诉我。干杯。
如ReactiveSwift's slack channel中所述,这是预期的结果。
The outer event stream is started observed. Each subsequent event stream is not observed until the preceeding one has completed.
所以 numbersSignal
只会在 lettersObserver
完成后发送值。