Thursday, November 7, 2024

Problematic Swift Concurrency Patterns

Matt Massicotte:

I think the vast majority of the time, a global actor should be applied to the type as a whole, not to individual properties.

[…]

But, while detached does prevent isolation inheritance, it also does other stuff too. Detached tasks do not inherit priority or task-local values. Instead, think about a nonisolated function.

[…]

I think MainActor.run is rarely the right solution.

[…]

Moving from completion handlers to async methods can change semantics and cause code to run on background threads. Be really careful here!

[…]

The compiler will automatically generate async versions of Objective-C completion handler-based methods. The bad news is, unless the type itself is MainActor-isolated or Sendable, these translations will be problematic. They won’t be possible to use diagnostic-free without a @preconcurrency import and will have different semantics that could make them unsafe.

Previously:

1 Comment RSS · Twitter · Mastodon


I appreciate Matt's writeups and being a forerunner on this stuff. He's been very helpful.

Everything I read here is a mess. This is exactly what I don't want to deal with. I can't wrap my head around a lot of it.

I'm going to stick with GCD + queues or locks and non-Swift langs. The patterns and pitfalls are well-understood. Keep it simple.

Leave a Comment