Subject
@propertyWrapper
public class Subject<Element> : Subscribable, Observer, ObservableConvertible
A subscribable source that can emit events to its observers and be an observer itself.
You can use it as a property wrapper to make the emission private in the class where it’s contained, but still expose an Observable
to the other classes.
-
Creates a new subject.
Declaration
Swift
public init() -
The wrapped observable value for property wrapper usage.
Declaration
Swift
public var wrappedValue: Observable<Element> { get } -
Emits an element to all subscribers.
Declaration
Swift
public func onNext(_ element: Element)Parameters
elementThe element to emit.
-
Completes this subject, calling
onCompleteon all current subscribers. After completion, new subscribers immediately receiveonComplete.Declaration
Swift
public func onComplete() -
Declaration
Swift
public func asObservable() -> Observable<Element> -
Declaration
Swift
public func subscribe<O>(_ observer: O) -> any Disposable where Element == O.Element, O : Observer -
Subscribes the observer only once and then automatically disposes it.
This is meant to be used when you only need one observer to be registered once. Use the standalone
first()operator if multiple observers all need to register for one event.Declaration
Swift
@discardableResult func subscribeOnce(_ observer: @escaping (Element) -> Void) -> any DisposableReturn Value
a
Disposablethat can be used to dispose this observer before the first event is sent to the observer, in case it’s not needed any longer.
-
Emits a
Voidevent to all subscribers.Declaration
Swift
func onNext()
View on GitHub