Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Observing calling multiple times causes to trigger action multiple times. #203

Open
ios-dev-temper opened this issue Oct 18, 2018 · 1 comment

Comments

@ios-dev-temper
Copy link

ios-dev-temper commented Oct 18, 2018

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.

@sssuourabh
Copy link

sssuourabh commented Aug 20, 2020

I have used .updateSignal on ObservableComponent.
example -

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.
I hope it helps.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants