ChangeStream
public class ChangeStream<T> : CursorProtocol where T : Decodable, T : Encodable
extension ChangeStream: AsyncSequence, AsyncIteratorProtocol
A MongoDB change stream.
-
Undocumented
Declaration
Swift
public typealias Element = T -
Indicates whether this change stream has the potential to return more data.
This change stream will be dead after
nextreturnsnil, but it may still be alive aftertryNextreturnsnil.After either of
nextortryNextreturn a non-DecodingErrorerror, this change stream will be dead. It may still be alive after either returns aDecodingError, however.Warning
If this change stream is alive when it goes out of scope, it will leak resources. To ensure it is dead before it leaves scope, invokeChangeStream.kill(...)on it.Declaration
Swift
public func isAlive() -> EventLoopFuture<Bool> -
The
ResumeTokenassociated with the most recent event seen by the change stream.Declaration
Swift
public internal(set) var resumeToken: ResumeToken? { get } -
Get the next
Tfrom this change stream.This method will continue polling until an event is returned from the server, an error occurs, or the change stream is killed. Each attempt to retrieve results will wait for a maximum of
maxAwaitTimeMS(specified on theChangeStreamOptionspassed to the method that created this change stream) before trying again.A thread from the driver’s internal thread pool will be occupied until the returned future is completed, so performance degradation is possible if the number of polling change streams is too close to the total number of threads in the thread pool. To configure the total number of threads in the pool, set the
MongoClientOptions.threadPoolSizeoption during client creation.Note: You must not call any change stream methods besides
killandisAlivewhile the future returned from this method is unresolved. Doing so will result in undefined behavior.If the future evaluates to an error, it is likely one of the following: -
MongoError.CommandErrorif an error occurs while fetching more results from the server. -MongoError.LogicErrorif this function is called after the change stream has died. -MongoError.LogicErrorif this function is called and the session associated with this change stream is inactive. -DecodingErrorif an error occurs decoding the server’s response.Declaration
Swift
public func next() -> EventLoopFuture<T?>Return Value
An
EventLoopFuture<T?>evaluating to the nextTin this change stream,nilif the change stream is exhausted, or an error if one occurred. The returned future will not resolve until one of those conditions is met, potentially after multiple requests to the server. -
Attempt to get the next
Tfrom this change stream, returningnilif there are no results.The change stream will wait server-side for a maximum of
maxAwaitTimeMS(specified on theChangeStreamOptionspassed to the method that created this change stream) before returningnil.This method may be called repeatedly while
isAliveis true to retrieve new data.Note: You must not call any change stream methods besides
killandisAlivewhile the future returned from this method is unresolved. Doing so will result in undefined behavior.Declaration
Swift
public func tryNext() -> EventLoopFuture<T?>Return Value
An
EventLoopFuture<T?>containing the nextTin this change stream, an error if one occurred, ornilif there was no data.If the future evaluates to an error, it is likely one of the following:
MongoError.CommandErrorif an error occurs while fetching more results from the server.MongoError.LogicErrorif this function is called after the change stream has died.MongoError.LogicErrorif this function is called and the session associated with this change stream is inactive.DecodingErrorif an error occurs decoding the server’s response.
-
Consolidate the currently available results of the change stream into an array of type
T.Since
toArraywill only fetch the currently available results, it may return more data if it is called again while the change stream is still alive.Note: You must not call any change stream methods besides
killandisAlivewhile the future returned from this method is unresolved. Doing so will result in undefined behavior.Declaration
Swift
public func toArray() -> EventLoopFuture<[T]>Return Value
An
EventLoopFuture<[T]>evaluating to the results currently available in this change stream, or an error.If the future evaluates to an error, that error is likely one of the following:
MongoError.CommandErrorif an error occurs while fetching more results from the server.MongoError.LogicErrorif this function is called after the change stream has died.MongoError.LogicErrorif this function is called and the session associated with this change stream is inactive.DecodingErrorif an error occurs decoding the server’s responses.
-
Calls the provided closure with each event in the change stream as it arrives.
A thread from the driver’s internal thread pool will be occupied until the returned future is completed, so performance degradation is possible if the number of polling change streams is too close to the total number of threads in the thread pool. To configure the total number of threads in the pool, set the
MongoClientOptions.threadPoolSizeoption during client creation.Note: You must not call any change stream methods besides
killandisAlivewhile the future returned from this method is unresolved. Doing so will result in undefined behavior.Declaration
Swift
public func forEach(_ body: @escaping (T) throws -> Void) -> EventLoopFuture<Void>Return Value
An
EventLoopFuture<Void>which will complete once the change stream is closed or once an error is encountered.If the future evaluates to an error, that error is likely one of the following:
MongoError.CommandErrorif an error occurs while fetching more results from the server.MongoError.LogicErrorif this function is called after the change stream has died.MongoError.LogicErrorif this function is called and the session associated with this change stream is inactive.
DecodingErrorif an error occurs decoding the server’s responses.
-
Kill this change stream.
This method MAY be called even if there are unresolved futures created from other
ChangeStreammethods.This method MAY be called if the change stream is already dead. It will have no effect.
Warning
On Swift versions and platforms where structured concurrency is not available, if a change stream is alive when it goes out of scope, it will leak resources. On those Swift versions/platforms, you must invoke this method to ensure resources are properly cleaned up. If structured concurrency is available, it is not necessary to call this method as resources will be cleaned up automatically during deinitialization.
Declaration
Swift
public func kill() -> EventLoopFuture<Void>Return Value
An
EventLoopFuturethat evaluates when the change stream has completed closing. This future should not fail. -
Declaration
Swift
public typealias AsyncIterator = ChangeStream -
Declaration
Swift
public func makeAsyncIterator() -> ChangeStream<T> -
Indicates whether this change stream has the potential to return more data.
This change stream will be dead after
next()returnsnil, but it may still be alive aftertryNext()returnsnil.After either of
next()ortryNext()throw a non-DecodingErrorerror, this change stream will be dead. It may still be alive after either returns aDecodingError, however.Declaration
Swift
public func isAlive() async throws -> Bool -
Get the next
Tfrom this change stream.This method will continue polling until an event is returned from the server, an error occurs, or the current
Taskis cancelled. Each attempt to retrieve results will wait for a maximum ofmaxAwaitTimeMS(specified on theChangeStreamOptionspassed to the method that created this change stream) before trying again.We recommend to run change streams in their own
Tasks, and to terminate them by cancelling theirTasks.Warning
You must not call any change stream methods besides
isAlive()while awaiting the result of this method. Doing so will result in undefined behavior.If an error is thrown, it is likely one of the following: -
MongoError.CommandErrorif an error occurs while fetching more results from the server. -MongoError.LogicErrorif this function is called after the change stream has been exhausted. -MongoError.LogicErrorif this function is called and the session associated with this change stream has been ended. -DecodingErrorif an error occurs decoding the server’s response to aT.Declaration
Swift
public func next() async throws -> T?Return Value
The next
Tin this change stream, ornilif the change stream is exhausted or the currentTaskis cancelled. This method will not return until one of those conditions is met, potentially after multiple requests to the server. -
Attempt to get the next
Tfrom the change stream, returningnilif there are no results.The change stream will wait server-side for a maximum of
maxAwaitTimeMS(specified on theChangeStreamOptionspassed to the method that created this change stream) before returningnil.This method may be called repeatedly while
isAlive()is true to retrieve new data.Warning
You must not call any change stream methods besides
isAlive()while awaiting the result of this method. Doing so will result in undefined behavior.If an error is thrown, it is likely one of the following: -
MongoError.CommandErrorif an error occurs while fetching more results from the server. -MongoError.LogicErrorif this function is called after the change stream has been exhausted. -MongoError.LogicErrorif this function is called and the session associated with this change stream has been ended. -DecodingErrorif an error occurs decoding the server’s response to aT.Declaration
Swift
public func tryNext() async throws -> T?Return Value
The next
Tin this change stream, ornilif there is no new data. -
Consolidate the currently available results of the change stream into an array of type
T.Since
toArraywill only fetch the currently available results, it may return more data if it is called again while the change stream is still alive.Warning
You must not call any change stream methods besides
isAlive()while awaiting the result of this method. Doing so will result in undefined behavior.If an error is thrown, it is likely one of the following: -
MongoError.CommandErrorif an error occurs while fetching more results from the server. -MongoError.LogicErrorif this function is called after the change stream has been exhausted. -MongoError.LogicErrorif this function is called and the session associated with this change stream has been ended. -DecodingErrorif an error occurs decoding the server’s responses toTs.Declaration
Swift
public func toArray() async throws -> [T]Return Value
An
Tcontaining the results currently available in this change stream.
View on GitHub
Install in Dash
ChangeStream Class Reference