Instance Methodswift-nio 2.72.0NIOEmbedded
executeInContext(_:)
Executes the given function in the context of this event loop. This is useful when it’s necessary to be confident that an operation is “blocking” the event loop. As long as you are executing, nothing else can execute in this loop.
AsyncTestingEventLoop.swift:283- iOS
- 13.0+
- macOS
- 10.15+
- tvOS
- 13.0+
- watchOS
- 6.0+
func executeInContext<ReturnType>(_ task: @escaping () throws -> ReturnType) async throws -> ReturnType where ReturnType : Sendable
While this call is running, no action can take place on the loop. This function can therefore be a good place to schedule a bunch of tasks “at once”, with a guarantee that none of them can progress. It’s also useful if you have types that can only be safely accessed from the event loop thread and want to be 100% sure of the thread-safety of accessing them.
Be careful not to try to spin the event loop again from within this callback, however. As long as this function is on the call stack the NIOAsyncTestingEventLoop
cannot progress, and so any attempt to progress it will block until this function returns.
Other members in extension
Type members
init(
) Initialize a new
NIOAsyncTestingEventLoop
.
Instance members
var inEventLoop: Bool
func advanceTime(by: TimeAmount
) async Runs the event loop and moves “time” forward by the given amount, running any scheduled tasks that need to be run.
func advanceTime(to: NIODeadline
) async Runs the event loop and moves “time” forward to the given point in time, running any scheduled tasks that need to be run.
func execute(@escaping () -> Void
) On an
NIOAsyncTestingEventLoop
,execute
will simply usescheduleTask
with a deadline of now. Unlike with the other operations, this will immediately execute, to eliminate a common class of bugs.func preconditionInEventLoop(file: StaticString, line: UInt
) func preconditionNotInEventLoop(file: StaticString, line: UInt
) func run(
) async Run all tasks that have previously been submitted to this
NIOAsyncTestingEventLoop
, either by callingexecute
or events that have been enqueued usingscheduleTask
/scheduleRepeatedTask
/scheduleRepeatedAsyncTask
and whose deadlines have expired.func scheduleTask<T>(deadline: NIODeadline, @escaping () throws -> T
) -> Scheduled<T> func scheduleTask<T>(in: TimeAmount, @escaping () throws -> T
) -> Scheduled<T> func shutdownGracefully(
) async The concurrency-aware equivalent of
shutdownGracefully(queue:_:)
.func shutdownGracefully(queue: DispatchQueue, @escaping (Error?) -> Void
)
Show implementation details (4)
Hide implementation details
func _preconditionSafeToSyncShutdown(file: StaticString, line: UInt
) func _preconditionSafeToWait(file: StaticString, line: UInt
) func _promiseCompleted(futureIdentifier: _NIOEventLoopFutureIdentifier
) -> (file: StaticString, line: UInt)? func _promiseCreated(futureIdentifier: _NIOEventLoopFutureIdentifier, file: StaticString, line: UInt
)