【发布时间】:2021-07-16 21:11:44
【问题描述】:
我观察到有关 CombineLatest 的意外行为,如果内部发布者有 subscribe(on:),则 CombineLatest 流不会发出任何值。
注意事项:
- Zip 操作员正在工作
- 将 subscribe(on:) / receive(on:) 移动到 combineLatest 流也可以。但是在这个特定的用例中,内部发布者正在定义他们的订阅/接收 因为在其他地方被(重新)使用了。
- 仅将 subscribe(on:)/receive(on:) 添加到其中一个内部发布者也可以,所以问题在于两者都有。
func makePublisher() -> AnyPublisher<Int, Never> {
Deferred {
Future { promise in
DispatchQueue.global(qos: .background).asyncAfter(deadline: .now() + 3) {
promise(.success(Int.random(in: 0...3)))
}
}
}
.subscribe(on: DispatchQueue.global())
.receive(on: DispatchQueue.main)
.eraseToAnyPublisher()
}
var cancellables = Set<AnyCancellable>()
Publishers.CombineLatest(
makePublisher(),
makePublisher()
)
.sink { completion in
print(completion)
} receiveValue: { (a, b) in
print(a, b)
}.store(in: &cancellables)
这是一个组合错误还是预期的行为?您知道如何设置这种内部人员可以定义自己的订阅调度程序的流吗?
【问题讨论】:
-
你有什么问题?
-
@NewDev 刚刚更新了问题
标签: ios swift combine combinelatest