Search code examples
swiftuinavigationcontrollerrx-swift

RxSwift - Replayed Tap Events


When the user taps on a button I display a selection screen. The result is passed into my view model. The code looks like this:

locationButton.rx.tap
            .flatMapLatest(wireframe.displayPlaceSelection)
            .filterNil()
            .map(DiscoverLocationSelection.location)
            .subscribe(onNext: viewModel.updateLocation(with:))
            .disposed(by: disposeBag)

This code works fine, and the user may select a location and will be returned to this screen with the button.

If the user then taps the button a second time, the displayPlaceSelection() function is called twice. If the user were to do this a third time, the function will be called 3 times. This results in multiple view controllers being pushed on top of each other.

How can I stop these events from stacking up?

The effect is this:

RxSwift Tap Replay


Solution

  • I can suppose that this code (subscribe) is called every time when you open this vc and thus you have many subscribers to the tap event. Where is this code? viewDidLoad or viewWillAppear for example.