ChangeStream

public class ChangeStream<T> : CursorProtocol where T : Decodable, T : Encodable

A MongoDB change stream.

  • The ResumeToken associated with the most recent event seen by the change stream.

    Declaration

    Swift

    public var resumeToken: ResumeToken? { get }
  • Indicates whether this change stream has the potential to return more data.

    This change stream will be dead after next returns nil, but it may still be alive after tryNext returns nil.

    After either of next or tryNext return a non-DecodingError error, this change stream will be dead. It may still be alive after either returns a DecodingError, however.

    Declaration

    Swift

    public func isAlive() -> Bool
  • Get the next T from this change stream.

    This method will block until an event is returned from the server, an error occurred, or the change stream is killed. Each attempt to retrieve results will wait server-side for a maximum of maxAwaitTimeMS (specified on the ChangeStreamOptions passed to the method that created this change stream) before making another request.

    A thread from the pool will be occupied by this method until it returns, 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.threadPoolSize option on client creation.

    If the result contains an error, it is likely one of the following: - MongoError.CommandError if an error occurs while fetching more results from the server. - MongoError.LogicError if this function is called after the change stream has died. - MongoError.LogicError if this function is called and the session associated with this change stream is inactive. - DecodingError if an error occurs decoding the server’s response.

    Declaration

    Swift

    public func next() -> Result<T, Error>?

    Return Value

    A Result<T, Error>? containing the next T in this change stream or an error if one occurred, or nil if the change stream is exhausted. This method will block until one of those conditions is met, potentially after multiple requests to the server.

  • Attempt to get the next T from this change stream, returning nil if there are no results.

    The change stream will wait server-side for a maximum of maxAwaitTimeMS (specified on the ChangeStreamOptions passed to the method that created this change stream) before returning nil.

    This method may be called repeatedly while isAlive is true to retrieve new data.

    Declaration

    Swift

    public func tryNext() -> Result<T, Error>?

    Return Value

    A Result<T, Error>? containing the next T in this change stream, an error if one occurred, or nil if there was no data.

    If the result is an error, it is likely one of the following:

    • MongoError.CommandError if an error occurs while fetching more results from the server.
    • MongoError.LogicError if this function is called after the change stream has died.
      • MongoError.LogicError if this function is called and the session associated with this change stream is inactive.
    • DecodingError if an error occurs decoding the server’s response.
  • Kill this change stream.

    This method may be called from another thread safely even if this change stream is blocked retrieving results. This is mainly useful for freeing a thread that the change stream is blocking with a long running operation.

    This method is automatically called in the deinit of ChangeStream, so it is not necessary to call it manually.

    This method will have no effect if the change stream is already dead.

    Declaration

    Swift

    public func kill()