ReactiveX / RxSwift

Reactive Programming in Swift
MIT License
24.37k stars 4.17k forks source link

BehaviorRelay.value not always synchronized with last value pushed using .accept() #2529

Closed Narayane closed 6 months ago

Narayane commented 1 year ago

Short description of the issue:

The BehaviorRelay attribute .value is sometimes not correctly updated after the use of .accept()

Expected outcome:

The BehaviorRelay attribute .value should always be equal to last value "pushed" using .accept()

What actually happens:

Sometimes yes, sometimes no, I do not know exactly why

Self contained code example that reproduces the issue:

 ViewModel

  var shouldAlertOnChangesNotSavedValue: Bool { self._shouldAlertOnChangesNotSaved.value }
  var shouldAlertOnChangesNotSaved: Observable<Bool> { self._shouldAlertOnChangesNotSaved.asObservable() }
  private var _shouldAlertOnChangesNotSaved = BehaviorRelay<Bool>(value: false)

   let navigationItemsObservable = self.outputs.navigationItems
            .filterEmpty()
            .map { $0.count }

  Observable.combineLatest(navigationItemsObservable, self.navigationController.hasOverviewPendingChanges)
            .do { [weak self] (count, hasPendingChanges) in
                let value = count > 1 && hasPendingChanges
                log.verbose("shouldAlertOnChangesNotSaved: \(value))") // true
                self?._shouldAlertOnChangesNotSaved.accept(value)
            }
            .catchErrorJustComplete()
            .disposed(by: self.disposeBag)

ViewController

  self.viewModel.outputs.shouldAlertOnChangesNotSaved
            .subscribe(onNext: { [weak self] value in
                self?.shouldAlertOnChangesNotSaved = value // false -> true
            })
            .disposed(by: self.disposeBag)

  @objc private func goBack() {
        log.verbose("BehaviorRelay.value: \(self.viewModel.outputs.shouldAlertOnChangesNotSavedValue)") // always false
        log.verbose("Observable.onNext: \(self.shouldAlertOnChangesNotSaved)") // true
        if self.shouldAlertOnChangesNotSaved {
            self.presentBackWithoutSavingAlert()
        } else {
            self.navigationController?.popViewController(animated: true)
        }
    }

RxSwift/RxCocoa/RxBlocking/RxTest version/commit

RxSwift 6.5.0

Platform/Environment

How easy is to reproduce? (chances of successful reproduce after running the self contained code)

Xcode version:

  14.3.1

Installation method:

I have multiple versions of Xcode installed: (so we can know if this is a potential cause of your issue)

Level of RxSwift knowledge: (this is so we can understand your level of knowledge and formulate the response in an appropriate manner)

freak4pc commented 1 year ago

Please create a more focused, runnable example of your issue (In an Xcode project) And share here, so we can check if it's a bug.

Thanks!

freak4pc commented 6 months ago

No response, closing.