CombineCommunity/rxswift-to-combine-cheatsheet

PublishSubject -> PassthroughSubject?

sshnip opened this issue · 5 comments

It's more like question not an issue. As I can see, in rxswift PublishSubject often used for binding + subscribing, e.g. the main idea of that class is to implement both Observer and Observable interfaces. In Combine this's no true because PassthroughSubject only implementes Pusblisher protocol and cannot be used as a subscriber. What do you think about that?

You can use a subject as a subscriber using this specific subscribe overload:
https://developer.apple.com/documentation/combine/publisher/subscribe(_:)-3fk20

@freak4pc Thanks for a good point! I've tried to make a simple implementation of this method and it worked with simple example. Now I'm struggling with another thing.
My desired result is to use parent -> child objects which should be connected using Publishers and Subscribers. Sometimes I need to use some intermediate actions, like PassthroughSubject<CustomType, Never>.map... and subscribe another PassthroughSubject<String, Never> to the result of this mapping. So in the end I couldn't find the proper way to erase result of map operator, there's no eraseToAnySubject() atm. I would like to implement it myself but I don't really know where to start from. Is there anything ready-to-use for my case in current Combine API?
PS: I've actively used such approach using RxSwift but atm I couldn't use it.

Thanks in advance

What did you do in RxSwift to achieve this? @sshnip

Hi @freak4pc
Sorry for the delay, here's the sample and I couldn't figure out the good way to do the same in combine. You could ignore BehaviorRelay, it could be any kind of publisher. So I would like to make a subscription in a similar manner, after doing map and without that as well let initalReplay = BehaviorRelay(value: 1)

        let intermediateReplay = ReplaySubject<String?>.create(bufferSize: 1)
        let finalSubject = PublishSubject<String?>()
        let anotherSubj = PublishSubject<Int>()
        initalReplay
            .map({ (value) -> String? in
                return String(describing: value)
            })
            .subscribe(intermediateReplay)
            .disposed(by: disposables)
        intermediateReplay
            .share(replay: 1, scope: .forever)
            .subscribe(finalSubject)
            .disposed(by: disposables)
        initalReplay
            .subscribe(anotherSubj)
            .disposed(by: disposables)
        finalSubject
            .subscribe { (value) in
                print(value)
            }
            .disposed(by: disposables)
        anotherSubj
            .subscribe { (value) in
                print("Another subj received \(String(describing: value))")
            }
            .disposed(by: disposables)
        initalReplay.accept(5)
        initalReplay.accept(6)
        initalReplay.accept(7)

This is quite a lot of code with multiple players. Mind focusing this up a bit more on the portion where this issue relates? I mean the relation with Passthrogh and Publish?

If it's a general Combine question I suggest posting in Slack, more peopel look there. http://slack.combine.community