subscribe(to:onNext:onComplete:)

Subscribes to receive a service’s instances whenever they change.

ServiceDiscovery.swift:68
@preconcurrency func subscribe(to service: Service, onNext nextResultHandler: @escaping (Result<[Instance], Error>) -> Void, onComplete completionHandler: @escaping (CompletionReason) -> Void) -> CancellationToken

Parameters

service

The service to subscribe to

nextResultHandler

The closure to receive update result

completionHandler

The closure to invoke when the subscription completes (e.g., when the ServiceDiscovery instance exits, etc.), including cancellation requested through CancellationToken.

Returns

A CancellationToken instance that can be used to cancel the subscription in the future.

The service’s current list of instances will be sent to nextResultHandler when this method is first called. Subsequently, nextResultHandler will only be invoked when the service’s instances change.

Threading

nextResultHandler and completionHandler may be invoked on arbitrary threads, as determined by implementation.