assumeIsolated(_:file:line:)
Assume that the current task is executing on the main actor’s serial executor, or stop program execution.
Declaration
static func assumeIsolated<T>(_ operation: @MainActor () throws -> T, file: StaticString = #fileID, line: UInt = #line) rethrows -> T where T : SendableParameters
- operation:
The operation that will be executed if the current context is executing on the MainActor’s serial executor.
- file:
The file name to print if the assertion fails. The default is where this method was called.
- line:
The line number to print if the assertion fails The default is where this method was called.
Return Value
The return value of the operation
Discussion
This method allows to assume and verify that the currently executing synchronous function is actually executing on the serial executor of the MainActor.
If that is the case, the operation is invoked with an isolated version of the actor, / allowing synchronous access to actor local state without hopping through asynchronous boundaries.
If the current context is not running on the actor’s serial executor, or if the actor is a reference to a remote actor, this method will crash with a fatal error (similar to preconditionIsolated()).
This method can only be used from synchronous functions, as asynchronous functions should instead perform a normal method call to the actor, which will hop task execution to the target actor if necessary.