Swift 结合2个出版主题';s并以可观察的方式发射

Swift 结合2个出版主题';s并以可观察的方式发射,swift,rx-swift,reactive-swift,Swift,Rx Swift,Reactive Swift,我的服务中有两个独立的数据集合 特色和标准内容 我有两个api调用来返回这些项目。它们可以单独使用,但是当我想要获取这两组数据,根据条件提供一些充实,然后将它们返回给消费者时,我也有用例 我希望我能做这样的事情: 类ContentService:ContentServiceType{ let featured=PublishSubject() 让标准=PublishSubject() 让内容:可观察 私人出租客户:客户 私有出租dispebag=dispebag() 初始化(客户端:客户端){

我的服务中有两个独立的数据集合

特色
标准
内容

我有两个api调用来返回这些项目。它们可以单独使用,但是当我想要获取这两组数据,根据条件提供一些充实,然后将它们返回给消费者时,我也有用例

我希望我能做这样的事情:


类ContentService:ContentServiceType{
let featured=PublishSubject()
让标准=PublishSubject()
让内容:可观察
私人出租客户:客户
私有出租dispebag=dispebag()
初始化(客户端:客户端){
self.client=client
内容=可观察
.CombineTest(特色、标准)
.map{(特色,标准)->(特色:[Content],标准:[Content])中的
/*
进行一些扩展并创建,然后返回新的更新版本
*/
退货(特色:updatedFeatured,标准:updatedStandard)
}.share()
}
func fetchStandardContent(页面:Int=0,大小:Int=100)->Single{
设params=[“page”:page,“size”:size]
let请求:Single=client.request(.getStandardContent(params))
return request.map{[unowned self]启动器位于
self.standard.onNext(content.props)
返回()
}
}
func fetchFeaturedContent(页面:Int=0,大小:Int=100)->Single{
设params=[“page”:page,“size”:size]
let请求:Single=client.request(.getFeaturedContent(params))
return request.map{[unowned self]中的内容
self.featured.onNext(content.props)
返回()
}
}
}   
在我的应用程序的其他地方,我当时希望我能做一些类似的事情

contentSvc.content
.observeOn(MainScheduler.instance)
.subscribeOn(ConcurrentDispatchQueueScheduler(qos:.后台))
.subscribe(onNext:{中的内容)
/*做一些有内容的事情*/
}).处置(由:处置人)
然后,每当调用
contentSvc.fetchFeaturedContent
contentSvc.fetchStandardContent
时,上面的
contentSvc.content
订阅者就会获得新数据


相反,
content
似乎没有发出任何值。

combinelatetest
要求两个源都发出,我相信它会自己发出


我可能会考虑使用
行为主题
行为关系
而不是PublishSubject。

组合相关测试
需要两个源发射,我相信它会发射自己


我可能会考虑使用
BehaviorSubject
BehaviorRelay
而不是PublishSubject。

我使用BehaviorRelay而不是PublishSubject,因为当从多个流绑定到PublishSubject(在应用程序之间共享)时,如果其中任何一个流发送完整的,PublishSubject可能会终止。中继类从不产生错误或从不完成

let featured = BehaviorRelay(value: [Content]())
let standard = BehaviorRelay(value: [Content]())

func getContent() -> Observable<(featured: [Content], standard: [Content])> {
     return Observable
       .combineLatest(
         featured.asObservable(),
         standard.asObservable(),
         resultSelector: { (featured, standard) -> (featured: [Content], standard: [Content]) in
           return (featured: featured, standard: standard)
       }
     )
  }

func addElemToFeatured() {
    featured.accept([Content(name: "abc")])
  }

  func addElemToStandard() {
    standard.accept([Content(name: "xyz")])
  }

我使用BehaviorRelay而不是PublishSubject,因为当从多个流绑定到PublishSubject(在应用程序之间共享)时,如果这些流中的任何一个发送完整的消息,PublishSubject可能会终止。中继类从不产生错误或从不完成

let featured = BehaviorRelay(value: [Content]())
let standard = BehaviorRelay(value: [Content]())

func getContent() -> Observable<(featured: [Content], standard: [Content])> {
     return Observable
       .combineLatest(
         featured.asObservable(),
         standard.asObservable(),
         resultSelector: { (featured, standard) -> (featured: [Content], standard: [Content]) in
           return (featured: featured, standard: standard)
       }
     )
  }

func addElemToFeatured() {
    featured.accept([Content(name: "abc")])
  }

  func addElemToStandard() {
    standard.accept([Content(name: "xyz")])
  }

或者他可以使用
startWith
方法。或者他可以使用
startWith
方法。