Problematic Swift Concurrency Patterns

submited by
Style Pass
2024-11-07 23:30:04

Recently, someone asked me about best practices when using Swift concurrency. I have mixed feelings about the concept of a “best practice”. I’ll get to that in a bit. But, in this case, I said that the technology is still very young and we’re all still figuring it out.

But, I then went on to say that I have now come across a number of techniques that often don’t work out. This does not mean bad! It just means that I see them a fair bit, and they often end up being a problem. I thought it could be useful to collect and share what I’ve seen.

A type like this is problematic, because half of it is non-isolated, and half is MainActor-only. But, that’s really weird! Because this type isn’t Sendable, if you were to ever create one off of the MainActor, it could never be later moved back to the MainActor, making value inaccessible.

There might be advanced/clever usages of such a type. But I think the vast majority of the time, a global actor should be applied to the type as a whole, not to individual properties.

Leave a Comment