# Task and Task.detached

**URL:** <https://forums.swift.org/t/task-and-task-detached/80861>\
**Category:** Using Swift\
**Tags:** concurrency\
**Created:** [July 2, 2025, 3:14pm UTC](https://forums.swift.org/t/task-and-task-detached/80861 "2025-07-02T15:14:22Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![somu](https://avatars.discourse-cdn.com/v4/letter/s/ce73a5/32.png) [@somu](https://forums.swift.org/u/somu)\
**Post date:** [July 2, 2025, 3:14pm UTC](https://forums.swift.org/t/task-and-task-detached/80861/1 "2025-07-02T15:14:22Z")

</div>

Hi,

I have been reading / watching videos about swift concurrency in `Swift 6.2` and concepts such as:

- `@MainActor` - Run on main thread
- `@concurrent` - Run on background thread (hop off hop off calling actor executor)
- `nonisolated(nonsending)` - Will continue on the caller's executor

# Questions

1. How does `Task { }` and `Task.detached { }` fit with the above concepts?
2. Will task's closure `Task { }` be executed in the caller's executor?
3. Will task's closure `Task.detached { }` be executed in the background thread?

---

<div class="post-metadata">

**Author:** ![bbrk24](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/bbrk24/32/27370_2.png) [@bbrk24](https://forums.swift.org/u/bbrk24)\
**Post date:** [July 2, 2025, 5:23pm UTC](https://forums.swift.org/t/task-and-task-detached/80861/2 "2025-07-02T17:23:48Z")

</div>

> [@somu](#):
>
> Will task's closure `Task { }` be executed in the caller's executor?

My understanding is that — by default — it does if it’s created in a statically isolated function, but not if it’s in a `nonisolated(nonsending)` function. That being said, both `Task` and `Task.detached` can explicitly specify where they run, e.g. `Task { @MainActor in … }`.

---

<div class="post-metadata">

**Author:** ![somu](https://avatars.discourse-cdn.com/v4/letter/s/ce73a5/32.png) [@somu](https://forums.swift.org/u/somu)\
**Post date:** [July 2, 2025, 5:49pm UTC](https://forums.swift.org/t/task-and-task-detached/80861/3 "2025-07-02T17:49:00Z")

</div>

Thanks @bbrk24,

Yes of course explicitly stating can specify where it would run.

My question was referring to the behavior when not explicity stated.

## Based on my experimentation (correct me if I am wrong)

- `Task { }` inherits the context when it can, when in an actor, it can inherit the context and takes it, like when called from `@MainActor` runs on the main thread.
- `Task { }` can't inherit a context when called form a `nonisolated` function and therefore doesn't inherit context and therefore it runs on the background thread

---

<div class="post-metadata">

**Author:** ![David\_Smith](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/david_smith/32/37508_2.png) [@David\_Smith](https://forums.swift.org/u/David_Smith)\
**Post date:** [July 2, 2025, 8:42pm UTC](https://forums.swift.org/t/task-and-task-detached/80861/4 "2025-07-02T20:42:07Z")

</div>

This looks correct to me.

It's also worth noting that detached Tasks have a bunch of other effects besides changing context inheritance, including not sharing Task-local variables with the parent, and not inheriting priority in the same way.

Because of this I'm always pretty skeptical of using `detached` just to disable inheritance; I would generally prefer moving the code to an `@concurrent async` function and calling that from the `Task` (or eliminating the Task entirely if the caller is also async).

If you're familiar with `DispatchQueue`, the equivalent concept is [DISPATCH\_BLOCK\_DETACHED | Apple Developer Documentation](https://developer.apple.com/documentation/dispatch/dispatch_block_flags_t/dispatch_block_detached)

---

<div class="post-metadata">

**Author:** ![somu](https://avatars.discourse-cdn.com/v4/letter/s/ce73a5/32.png) [@somu](https://forums.swift.org/u/somu)\
**Post date:** [July 3, 2025, 1:28am UTC](https://forums.swift.org/t/task-and-task-detached/80861/5 "2025-07-03T01:28:53Z")

</div>

@David_Smith thanks a lot!!

That is a very clear explanation and thanks for order of preference.

---

<div class="post-metadata">

**Author:** ![Gero](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/gero/32/23571_2.png) [@Gero](https://forums.swift.org/u/Gero)\
**Post date:** [July 3, 2025, 9:36am UTC](https://forums.swift.org/t/task-and-task-detached/80861/6 "2025-07-03T09:36:51Z")

</div>

> [@somu](#):
>
> `Task { }` can't inherit a context when called form a `nonisolated` function and therefore doesn't inherit context and therefore it runs on the background thread

I've never thought about this, but that's interesting with the changed behavior of `nonisolated(nonsending)`... I mean, during runtime we're _always_ on _some_ isolation context, so what does "can't inherit" mean? Is it dynamically determined, i.e. the `Task`'s closure runs on whatever isolation context the function runs on (which was determined by its caller, up to a root task)? Or is it always a _new_ isolation context?
