3

I have a singleton that's in charge of handling user parameter changes for logic operations across my app. The parameters are divided into two types, "influencing" and "steering", and each is represented by a CurrentValueSubject.

In my central controller class I use CombineLatest to update all my parameters whenever either type of parameter changes. From the controller's init():

Publishers
    .CombineLatest(
        InteractionParameters.sharedInstance.influenceParameters,
        InteractionParameters.sharedInstance.steeringParameters
    )
    .debounce(for: 0.3, scheduler: DispatchQueue.main)
    .sink { (influencing, steering) in
        print("influencing params: \(influencing), steering: \(steering)")
    }
    .store(in: &subscriptions)

This works, in that I do get the changes as they're made by the user. But I always get all the parameters twice, in immediate succession — i.e., the output here is always printed twice. I've double-checked the debounce, which seems to work as expected — the value being sent twice is always the last value to hit the debounce.

I'm sure it's some simple mistake/misunderstanding, but not sure how to fix it.

Thanks in advance.

jbm
  • 1,248
  • 10
  • 22

1 Answers1

2

Okay, user error, I'm afraid... Ugh...

I'd inadvertently created two of these controller objects, and although one wasn't being used anywhere, its subscription was (of course) created, leading to the strange (looking) behaviour.

As a note that might help others, inserting a handleEvents():

Publishers
            .CombineLatest(InteractionParameters.sharedInstance.influenceParameters, InteractionParameters.sharedInstance.steeringParameters)
            .handleEvents(receiveSubscription: { print("Receive subscription: \($0)") },
                          receiveOutput: { print("Receive output: \($0)") })
            .debounce(for: 0.3, scheduler: DispatchQueue.main)
            .sink { (influencing, steering) in
                print("influencing params: \(influencing), steering: \(steering)")
            }
            .store(in: &subscriptions)

helped me see the two subscriptions being created, which led me to my mistake.

jbm
  • 1,248
  • 10
  • 22