我有一个类似于下面操场示例中的出版商设置.
import Combine
let sub1 = PassthroughSubject<String, Never>().prepend("initial 1")//.share()
let sub2 = PassthroughSubject<String, Never>().prepend("initial 2")
Publishers.CombineLatest(sub1, sub2).sink { content1, content2 in
print("combined received: \(content1) and \(content2)")
}
sub1.sink { content1 in
print("first received: \(content1)")
}
sub2.sink { content2 in
print("second received: \(content2)")
}
如果sub1
后面的share()
被注释掉,控制台将打印以下内容:
combined received: initial 1 and initial 2
first received: initial 1
second received: initial 2
这似乎在意料之中.但如果存在share()
,就会产生意想不到的印记:
combined received: initial 1 and initial 2
second received: initial 2
在我的XCode项目中,我使用prepend()
来触发初始执行,但是share()
会阻止我的一些链的初始执行.
这是预期中的行为吗?