Difference between Task and async

Given:

def logMessage(message: String) async { ... }

What is the difference between these three snippets of code:

async { logMessage("done") } 
Task { logMessage("done") } 
Task.detached { logMessage("done") } 

I've read through the concurrency proposals, but can't quite resolve this.

async{} deprecated spelling that will be removed and is functionally equal to Task{}.

For details, you could refer to the „Explore structured concurrency in Swift” talk from WWDC 2021, if reading the proposal’s section on unstructured tasks still has left you with questions.

The short version is:

  • Task{} is unstructured and does inherit actor execution context, task local values, task priority etc
  • Task.detached{} is unstructured and DOES NOT inherit anything.

Hope this helps,

5 Likes

Thanks - that helps!

So if both Task and Task.detached are unstructured and async is going away, then is TaskGroup the only kind of "structured" concurrency?

Also, I'm not able to use Task in this way in Xcode 13 beta 2. Is that coming in a future release?

And async let which is really the primary API for it.

Yeah I think beta 2 still misses some of those changes; expect them to be fixed/aligned in the next beta. :pray:

Oh, of course! I forgot about that. Thanks again!

1 Like
Terms of Service

Privacy Policy

Cookie Policy