CombineSchedulers Documentation

Function run()

public func run()  

Runs the scheduler until it has no scheduled items left.

This method is useful for proving exhaustively that your publisher eventually completes and does not run forever. For example, the following code will run an infinite loop forever because the timer never finishes:

let scheduler = DispatchQueue.test
Publishers.Timer(every: .seconds(1), scheduler: scheduler)
  .autoconnect()
  .sink { _ in print($0) }
  .store(in: &cancellables)

scheduler.run() // Will never complete

If you wanted to make sure that this publisher eventually completes you would need to chain on another operator that completes it when a certain condition is met. This can be done in many ways, such as using prefix:

let scheduler = DispatchQueue.test
Publishers.Timer(every: .seconds(1), scheduler: scheduler)
  .autoconnect()
  .prefix(3)
  .sink { _ in print($0) }
  .store(in: &cancellables)

scheduler.run() // Prints 3 times and completes.
@MainActor
    public func run()