Swift nonsending async

스위프트 호출자 격리 비동기 함수

Continue on the caller actor, or opt into a separate executor with @concurrent.

···
html
<div class="v"><header><b>ASYNC EXECUTOR CHOICE</b><span id="state"></span></header><main><div class="choice"><div class="row" id="actorrow"><b>CALLER ACTOR</b><span id="actorjob">await → work</span></div><div class="row" id="poolrow"><b>POOL</b><span id="pooljob">idle</span></div></div></main><footer>execution context is an explicit choice</footer></div>
css
.v{width:min(92vw,660px);height:min(86vh,310px);box-sizing:border-box;padding:clamp(9px,2.5vmin,18px);border:1px solid var(--line);border-radius:14px;background:var(--surface);display:flex;flex-direction:column;gap:7px;color:var(--fg);font:500 clamp(15px,3.2vmin,17px)/1.25 var(--font-sans, sans-serif)}.v header,.v footer{display:flex;justify-content:space-between;align-items:center;gap:8px;white-space:nowrap}.v header b{color:var(--accent);font-size:.9em}.v header span,.v footer{color:var(--muted);font-size:.82em}.v main{flex:1;min-height:0;position:relative;overflow:hidden}.v .mono{font-family:ui-monospace,SFMono-Regular,Consolas,monospace}.v .active{background:var(--accent)!important;color:var(--bg)!important;border-color:var(--accent)!important}.v .muted{opacity:.28} .choice{height:100%;display:flex;flex-direction:column;justify-content:center;gap:8px}.row{height:36%;box-sizing:border-box;border:1px solid var(--line);border-radius:7px;padding:7px;display:flex;align-items:center;justify-content:space-between;gap:5px;font:600 clamp(12px,2.7vmin,16px) var(--font-sans, sans-serif)}.row span{border:1px solid var(--line);border-radius:5px;padding:5px}.row.on{border-color:var(--accent)}.row.on span{background:var(--accent);color:var(--bg)}
js
let n=0;function draw(){document.getElementById("actorrow").classList.toggle("on",!n);document.getElementById("poolrow").classList.toggle("on",!!n);document.getElementById("actorjob").textContent=n?"caller free":"await → work";document.getElementById("pooljob").textContent=n?"@concurrent work":"idle";document.getElementById("state").textContent=n?"explicit hop":"nonsending";n=1-n}draw();setInterval(draw,1350)

Swift 6.2 nonisolated(nonsending) async functions can inherit the caller actor execution context. This avoids an unnecessary hop and lets the function work safely with values isolated there.

CPU-heavy work can still hold up a UI actor. Mark work that should run on another executor with @concurrent. Check project language settings because the default behavior depends on adopted features.

When to use

Use it when deciding whether an async function stays with its caller or runs concurrently.

Open as page ↗