Generic Instance Method

subscribe(on:options:)

Specifies the scheduler on which to perform subscribe, cancel, and request operations.

Declaration

func subscribe<S>(on scheduler: S, options: S.SchedulerOptions? = nil) -> Publishers.SubscribeOn<Publishers.Autoconnect<Upstream>, 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:options:), which affects downstream messages, subscribe(on:) changes the execution context of upstream messages. In the following example, requests to jsonPublisher are performed on backgroundQueue, but elements received from it are performed on RunLoop.main.

let ioPerformingPublisher == // Some publisher.
let uiUpdatingSubscriber == // Some subscriber that updates the UI.

ioPerformingPublisher
    .subscribe(on: backgroundQueue)
    .receiveOn(on: RunLoop.main)
    .subscribe(uiUpdatingSubscriber)