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.Share<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)

Beta Software

This documentation contains preliminary information about an API or technology in development. This information is subject to change, and software implemented according to this documentation should be tested with final operating system software.

Learn more about using Apple's beta software