withMainSerialExecutor(operation:)
Perform an operation on the main serial executor.
@MainActor func withMainSerialExecutor(operation: () async throws -> Void) async rethrows
Parameters
- operation
An operation to be performed on the main serial executor.
Some asynchronous code is notoriously difficult to test in Swift due to how suspension points are processed by the runtime. This function attempts to run all tasks spawned in the given operation serially and deterministically. It makes asynchronous tests faster and less flakey.
await withMainSerialExecutor {
// Everything performed in this scope is performed serially...
}
See Reliably testing async code for more information on why this tool is needed to test async code and how to use it.