I have a file with some constants that maps enums to associated ImageResource
for simplified usage in other places. Sample is below
static let imagePreviews: [MyEnum: ImageResource] = [
.Enum1: .imageResource1,
.Enum1: .imageResource2,
...
]
After upgrading to latest XCode that uses Swift 5.10 following warning is shown around code block above and judging by other similar warnings, common pattern is usage of ImageResource
Static property ‘activityBlockImagePreviews’ is not concurrency-safe
because it is not either conforming to ‘Sendable’ or isolated to a
global actor; this is an error in Swift 6
2
Answers
You can use global actors to control access
This will add a whole other layer of complexity to the app because of the class, struct, method that calls isn’t already on the same actor you will have to await.
You don’t have to use
MainActor
you can create your own.The issue is that you have a
static
(or a global) that represents an unsynchronized, shared mutable state. If this was your own type, you could make itSendable
. (See WWDC 2022 video Eliminate data races using Swift Concurrency.)But I presume you are using the framework’s
ImageResource
type, which is notSendable
. So, you could fix this by isolating it to a global actor. E.g., if you wanted to isolate it to the main actor:And then: