Migrating the TelemetryDeck SDK to Swift 6 Mode
And this summer at WWDC 2024 the longest session of them all was migrating your app to Swift 6 for a reason. This major new update to the language brings a new level of safety – namely data-race safety – which is awesome news for more correct code, but it also comes with a lot of new requirements we all need to adapt to.
[…]
The following 3 sections explain how we fixed the ~30 issues we've run into in our code, grouped by the solution we applied and an explanation why we opted for that solution with a code sample.
[…]
As
ISO8601DateFormatter
is a type defined within Foundation, we can't make it concurrency-safe itself, so we just need to deal with its mutable nature and work around it. The easiest way to this in our case was to turn ourlet
constant into a get-only computed property, like so[…]
I find this aesthetically displeasing. It would be unusual and bad form to modify the properties of a shared formatter. But there’s no way to tell Swift you’ll treat it as immutable, so therefore we give every user a fresh copy? There’s probably a way to wrap it in a Sendable
type, but how much do you want to contort your code to avoid this inefficiency?
Migrating to Swift 6 mode with all of its data-race safety glory was not an easy task. Despite our small project size, we ran into many warnings that all looked similar on the surface, but each of them needed careful consideration.
Previously:
- Swift Concurrency and Objective-C
- ISO8601DateFormatter and Fractional Seconds
- Unwanted Swift Concurrency Checking
- Swift 6
- Swift 6 Announced
Update (2024-10-03): Jesse Squires:
Ok so it appears someone has botched Swift Concurrency for
UICollectionViewDiffableDataSource
.How are you supposed to work around this?
1 Comment RSS · Twitter · Mastodon
Is this gonna be the feature that makes Swift kaputz?
The walls are closing in.