skip to Main Content

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


  1. You can use global actors to control access

    @MainActor static let imagePreviews: [MyEnum: ImageResource] = [
        .Enum1: .imageResource1,
        .Enum1: .imageResource2,
        ...
      ]
    

    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.

    await imagePreviews
    

    You don’t have to use MainActor you can create your own.

    Login or Signup to reply.
  2. 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 it Sendable. (See WWDC 2022 video Eliminate data races using Swift Concurrency.)

    But I presume you are using the framework’s ImageResource type, which is not Sendable. So, you could fix this by isolating it to a global actor. E.g., if you wanted to isolate it to the main actor:

    @MainActor
    extension ImageResource {
        static let imageResource1 = ImageResource(name: "foo", bundle: .main)
        static let imageResource2 = ImageResource(name: "bar", bundle: .main)
    }
    

    And then:

    @MainActor
    static let imagePreviews: [MyEnum: ImageResource] = [
        .foo: .imageResource1,
        .bar: .imageResource2
    ]
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search