Switch to background thread, then back to main once it's done

Afaik .detached detaches the Task from the current actor but if .background doesn't guarantee execution in the background either (tested it, still got the same warning), how do you actually push it to a background thread? DispatchQueue.global(qos: .background).async is an easy to use oneliner but people recommend to not use it anymore but what even is a good and easy alternative if a Task doesn't do it properly?

The settings under "Swift Compiler - Concurrency", and some of the settings under "Swift Compiler - Upcoming Features" can also be relevant for concurrency-related problems

Where did you find these settings?

In "Project - Build Settings" the only "Swift Compiler" sections are: Code Generation, Custom Flags, Language. No "Concurrency" or "Upcoming Features".

Could you provide a full reproducer? A detached task that isn’t explicitly @MainActor isolated shouldn’t execute on the @MainActor.

Specifying a TaskPriority of .background does, in most cases, enqueue the task (when the executor is a default or generic executor) on a dispatch queue whose QoS is equal to .background.

I'm not sure what to tell you here. If I go into "Build Settings" and search for concurrency, I see all these:

Scroll down. Towards the bottom there are the "Swift Compiler - Concurrency" and "Swift Compiler - Upcoming Features" sections. You can also use the filter field.

Sorry, reproducer?

Maybe it's the older Xcode version that I'm using? Updating Xcode often breaks something and I've not had the time to deal with that yet, so I haven't done it yet.

@Jon_Shier @bbrk24

Sorry about the delay again!

If I set the Build Settings to "All" (it was on "Customized" before), then I get this:

The options are: Minimal, Targeted, Complete

No "Swift Compiler - Concurrency" section though.

You're probably using a newer Xcode version?

What Xcode version are you on? I'm using Xcode 26.4.1 (which isn't quite the latest, since 26.6 is out by now).

16.4.

In my experience updating often breaks something in older projects and I've not had the time to deal with that yet, so I haven't updated yet.

A minimal, fully self-contained code sample that reproduces the issue.

I would strongly caution you against the use of Task.detached. It is a tool you should, essentially, never reach for. It's popular because it has a similar shape to dispatch-style concurrency. However, I think it is really a big red flag, even if you encounter in Apple-created projects.

I would say that Swit 6.1 is the oldest version of the compiler you can practically use to begin learning about the concurrency system. But, 6.2 would make this process significantly easier, because the NonisolatedNonsendingByDefault feature (AKA "Approachable Concurrency") probably will change the language semantics to better match how you expect them to work.

Also, AVFoundation is one of the known-difficult APIs when it comes to concurrency adoption. It is making process though, but because you are also using an older SDK, you are not getting these API improvements. Even with all those, using this framework with Swift 6 generally requires a very deep understanding of the concurrency system. I would not recommend it as a starting point, but that doesn't mean it is impossible. But if you aren't feeling confident yet in how to control what is and is not executed on the MainActor/non-MainActor, I think that is a necessary place to start.

Oh, you mean as a full download?

I posted the code I'm using in the very first comment but sorry, I can't provide a full project at the moment.

I would strongly caution you against the use of Task.detached.

Why is that? It was suggested in a couple of stackexchange answers but it didn't work, so I removed it again.

Swit 6.1

I'm using Swift 5 (5.5 I think). As I said in another comment: I don't want to upgrade Xcode for now (or even switch to a newer Swift version) because upgrading often breaks something and I just don't have the time to deal with that at the moment.

The weird thing is that all of this is working fine in Apple's sample project, which also uses Swift 5, so not sure if it's bugged in my project (I cleaned the build folder multiple times already) or if it's because they're using SwiftUI, while I'm using UIKit (I prefer to build UI in storyboard).

I would not recommend it as a starting point, but that doesn't mean it is impossible.

I've worked with Swift 5 before (around 1-2 years ago), so I've got some knowledge, but I was always using DispatchQueue.global(qos: .background).async before. AVFoundation is indeed quite complicated (the written tutorials didn't help much but the sample project was quite helpful) but it's something I "have" to work on at the moment, so no way around learning about Tasks too, I guess.

Because Task.detached makes it easier to defer thinking about the (necessary) transition towards controlling isolation via the type system. It looks too much like dispatch. But, it also does more than just cut off isolation inheritance, and while that isn't always a problem, it can be super hard to track down if/when that matters.

Xcode 16.4 includes Swift 6.1.4, but you are using it in the 5 language mode. This, combined with minimal checking, makes it very easy to put together a project that "works", but is actually invalid. I think Apple's AVFoundation sample code is a pretty good example of this phenomenon.

But I really do not want to discourage you. You absolutely can get this to work. @Jon_Shier 's recommendation of a custom actor executor seems to be a path that works well for people.