I have following RxSwift view model code:
private(set) var num = BehaviorRelay<Int>(value: 1)
private let indexTrigger = PublishRelay<Int>()
private let disposeBag = DisposeBag()
private func setupBindings() {
//...
self.num.distinctUntilChanged().bind(to: self.indexTrigger).disposed(by: self.disposeBag)
}
func numSelected(num: Int) {
self.num.accept(num)
}
This code is working fine and does what I want. I'm trying to do same, but with swift Combine framework with following code:
@Published private(set) var num: Int = 1
private let indexTrigger = PassthroughSubject<Int, Never>()
private var subscriptions = Set<AnyCancellable>()
private func setupBindings() {
//...
self.$num.removeDuplicates().sink(receiveValue: { [weak self] num in
self.indexTrigger.send(num)
}).store(in: &self.subscriptions)
}
func numSelected(num: Int) {
self.num = num
}
So RxSwift binding looks much clean and simple and without need of weak. I tried to check assign(on:) method in combine, but seems it is not the one. Is there way to do same thing?