Instance Propertyswift-nio 2.80.0NIOCore

loopBound

Returns this ChannelHandlerContext as a NIOLoopBound, bound to self.eventLoop.

ChannelPipeline.swift:2200
var loopBound: NIOLoopBound<ChannelHandlerContext> { get }

This is a shorthand for NIOLoopBound(self, eventLoop: self.eventLoop).

Being able to capture ChannelHandlerContexts in EventLoopFuture callbacks is important in SwiftNIO programs. Of course, this is not always safe because the EventLoopFuture callbacks may run on other threads. SwiftNIO programmers therefore always had to manually arrange for those callbacks to run on the correct EventLoop (i.e. context.eventLoop) which then made that construction safe.

Newer Swift versions contain a static feature to automatically detect data races which of course can’t detect the only dynamically EventLoop a EventLoopFuture callback is running on. NIOLoopBound can be used to prove to the compiler that this is safe and in case it is not, NIOLoopBound will trap at runtime. This is therefore dynamically enforce the correct behaviour.