是否有一个 rx 运算符可以合并流,但一次一个流?
我想将两个 observable
流合并为一个,就像使用 .merge()
运算符一样。我不使用该运算符的原因是它同时运行可观察对象。我需要一个合并两个流的运算符,但在开始第二个流之前完成第一个流,以便响应类似于: "stream1Value1", "stream1Value2", "stream2Value1", "stream2Value2".< /code>
使用 .merge()
运算符,代码看起来是这样的:
Observable.merge(self.getParameter(id: id).asObservable(), self.getSecondaryParameter(id: id).asObservable()).asSingle()
但给了我这样的输出: "stream1Value1", "stream2Value1", "stream1Value2", "stream2Value2".
我尝试过使用 Single.zip()
但这给了我两个参数
code> 来使用($0 和 $1)。另外我不确定它是否在开始第二个之前完成第一个......
I would like to merge two observable
streams into one, just like you do with the .merge()
operator. The reason I don't use that operator is because it runs the observables
at the same time. I need an operator that merges two streams, but finish the first before it starts with the second one, so that the response would be something like: "stream1Value1", "stream1Value2", "stream2Value1", "stream2Value2".
Using the .merge()
operator, the code looks lie this:
Observable.merge(self.getParameter(id: id).asObservable(), self.getSecondaryParameter(id: id).asObservable()).asSingle()
but gives me an output like this:"stream1Value1", "stream2Value1", "stream1Value2", "stream2Value2".
I have tried using Single.zip()
but this gives me two parameters
to work with ($0 and $1). Also I'm not sure if it finishes the first before starting with the second...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在我的文章中了解有关 RxSwift 中所有组合运算符的更多信息 在 RxSwift 中组合可观察量。
您正在寻找的运算符称为
concat
。有一个方法以及几个静态函数重载。在库中的“Concat.swift”文件中查找运算符的各种版本及其文档。
顺便说一句,
zip
运算符以及combineLatest
也会同时启动两个 Observable。Learn more about all the combining operators in RxSwift in my article Recipes for Combining Observables in RxSwift.
The operator you are looking for is called
concat
. There is a method as well as several static function overloads.Look in the "Concat.swift" file in the library for the various versions of the operator as well as their documentation.
BTW, the
zip
operator, as well ascombineLatest
, also start both Observables at the same time.