1

I have Singleton class to which i have used to observe a property and trigger next action.

Singleton Class:

public class BridgeDispatcher: NSObject {

    open var shouldRespondToBridgeEvent = SafePublishSubject<[String: Any]>()
    open var shouldPop = SafePublishSubject<Void>()
    open var shouldUpdate = SafePublishSubject<Void>()

    public let disposeBag = DisposeBag()

    open static let sharedInstance: BridgeDispatcher = BridgeDispatcher()

    override init() {
        super.init()

        shouldRespondToBridgeEvent.observeNext { event in
            if let type = event["type"] as? String {

                switch type {
                case "ShouldUpdate":
                        self.onShiftBlockDidUpdateHeight.next()
                case "shouldPop":
                    self.onPopCurrentViewController.next(())
                default:
                    print("Event not supported")
                }
            }
        }.dispose(in: self.disposeBag)
    }
}

Above method will trigger by calling:

BridgeDispatcher.sharedInstance.shouldRespondToBridgeEvent.next(body)

Register for onPopCurrentViewController:

BridgeDispatcher.sharedInstance.onPopCurrentViewController.observeNext { doSomething() }.dispose(in: BridgeDispatcher.sharedInstance.disposeBag)

On my application, BridgeDispatcher.sharedInstance.onPopCurrentViewController.observeNext{} method will be called multiple times due to the business logic, due to this doSomething() method will trigger multiple times when calling BridgeDispatcher.sharedInstance.shouldRespondToBridgeEvent.next(body).

Is this issue with my singleton design pattern or observeNext calling multiple times. (BridgeDispatcher.sharedInstance.onPopCurrentViewController.observeNext{} )

Need help.
caldera.sac
  • 4,918
  • 7
  • 37
  • 69

1 Answers1

0

I have used .updateSignal on ObservableComponent.

valueToUpdate.updateSignal.compactMap { (arg0) -> String? in
            let (value, _, validationFailure) = arg0
            return validationFailure == nil ? value?.value : nil
        }
        .removeDuplicates()
        .debounce(for: 1.0)
        .observeNext { [unowned self] _ in
            self.doYourWork()
        }
        .dispose(in: self.bag)

It attempts to deal with the multiple calls in two ways: first by discarding any duplicate events, so if the duration hasn’t changed, then no call is made. Second, by debouncing the signal so if the user makes a bunch of changes we only call the method when they’re done making changes.

Sourabh Shekhar
  • 157
  • 1
  • 12