Specifies the scheduler on which to perform subscribe, cancel, and request operations.
SDKs
- iOS 13.0+
- macOS 10.15+
- Mac Catalyst 13.0+
- tvOS 13.0+
- watchOS 6.0+
- Xcode 11.0+
Framework
- Combine
Declaration
func subscribe<S>(on scheduler: S, options: S.SchedulerOptions? = nil) -> Publishers.Subscribe On<Publishers.Map<Upstream, Output>, S> where S : Scheduler
Parameters
scheduler
The scheduler on which to receive upstream messages.
options
Options that customize the delivery of elements.
Return Value
A publisher which performs upstream operations on the specified scheduler.
Discussion
In contrast with receive(on:
, which affects downstream messages, subscribe(on:)
changes the execution context of upstream messages. In the following example, io
uses background
to process requests. However, it uses Run
to send elements to the subscriber.
let ioPerformingPublisher == // Some publisher.
let uiUpdatingSubscriber == // Some subscriber that updates the UI.
ioPerformingPublisher
.subscribe(on: backgroundQueue)
.receiveOn(on: RunLoop.main)
.subscribe(uiUpdatingSubscriber)