Swift Combine : 订阅在源发布者完成时取消主题

问题描述 投票:0回答:0

我有一个函数,它接收一个 Publisher 并创建一个用于两者的 PassthroughSubject

    • 订阅源发布者
    • send 手动值

例如:

class Adapter<T>{
  let innerSubject=PassThroughSubject<T,Never>()
  let scope = Scope() // custom type, Array<AnyCancellable>
  func init(_ source:Publisher<T,Never>){
    source.register(innerSubject).in(scope)
    innerSubject.sink(receiveValue:{debugPrint($0)}).in(scope)
  }
  func adapt(_ T val){
    innerSubject.send(val)
  }
}

fn usage(){
  let adapter=Adapter(Empty()) // change to Empty(completeImmediately:false) to fix
  adapter.adapt(42) // should print 42,
}

内部主题似乎被取消了,因为Empty calls complete。 我希望内部主题的 subscription 被取消,而不是主题本身。在 .Net 中至少是这样的行为——我是不是遗漏了什么?我需要将其包装在广播主题中吗?我认为发布者可以接收多个接收者并向每个接收者进行多播?

swift reactive-programming rx-swift combine
© www.soinside.com 2019 - 2024. All rights reserved.