15

In Apple's 2019 WWDC video Swift Combine in Practice, they demonstrate using a debounce publisher to slow down the rate of messages.

return $username
  .debounce(for: 0.5, scheduler: RunLoop.main)
  .removeDuplicates()
  .eraseToAnyPublisher()

However, anytime I attempt to use it in a similar fashion, I get the following error:

Cannot invoke 'debounce' with an argument list of type '(for: Double, scheduler: RunLoop)'

The debounce() signature is:

public func debounce<S>(for dueTime: S.SchedulerTimeType.Stride, 
                          scheduler: S,
                            options: S.SchedulerOptions? = nil) -> 
                                    Publishers.Debounce<Self, S> where S : Scheduler

SchedulerTimeType.Stride appears to be initializable with a numeric but it's not working for me or my inexperience with Swift Generics is on display.

What is the correct way to call this?

Edit

Duplicate of this question...

Searching for generic words like "Combine" is, for now, rather challenging...

macOS 10.15, Xcode 11

emma ray
  • 13,336
  • 1
  • 24
  • 50
kennyc
  • 5,490
  • 5
  • 34
  • 57
  • Also reported here: https://stackoverflow.com/q/56564967/1187415. – Apparently that is not yet implemented in the current beta release. – Martin R Jun 13 '19 at 11:48

1 Answers1

26

The documented debounce<S> operator accepts type S.SchedulerTimeType.Stride which looks something like this:

let sub = NotificationCenter.default
    .publisher(for: NSControl.textDidChangeNotification, object: filterField)
    .debounce(for: .milliseconds(500), scheduler: RunLoop.main)
    .subscribe(on: RunLoop.main)
    .assign(to:\MyViewModel.filterString, on: myViewModel)
emma ray
  • 13,336
  • 1
  • 24
  • 50
  • Thank you. I think the original issue was that Xcode 11 beta 1 did not support the `scheduler` argument. In the release notes for beta 2, it is now listed as being supported. The original error was about the invalid argument to `scheduler`, not the interval but it wasn't clear to me at the time. – kennyc Jun 20 '19 at 16:06
  • 1
    plus most of the code onscreen at WWDC doesn't actually compile - they tend to take a lot of creative liberty when making slides. – emma ray Jun 20 '19 at 16:23