Sixten Otto
Sixten Otto

Reputation: 14816

Are custom UIContentView / UIContentConfiguration implementations incompatible with Swift strict concurrency?

Let's say that I have this fairly minimal implementation of custom configuration and content view in my app:

struct ExampleContentConfiguration {
  let name: String
  let score: Int
}

extension ExampleContentConfiguration: UIContentConfiguration {
  func makeContentView() -> UIView & UIContentView {
    return ExampleContentView(configuration: self)  // [1]
  }
  
  func updated(for state: UIConfigurationState) -> ExampleContentConfiguration {
    return self
  }
}

final class ExampleContentView: UIView, UIContentView {
  var configuration: UIContentConfiguration  // [2]
  
  init(configuration: ExampleContentConfiguration) {
    self.configuration = configuration
    super.init(frame: .zero)
  }
  
  required init?(coder: NSCoder) {
    fatalError("init(coder:) has not been implemented")
  }
}

The two protocols that underlie this have no concurrency annotations. On the other hand, UIView (and its subclasses) are marked as @MainActor. So when I set the Strict Concurrency Checking build setting to Complete, Xcode reports two issues at the marked lines:

  1. Call to main actor-isolated initializer 'init(configuration:)' in a synchronous nonisolated context.
  2. Main actor-isolated property 'configuration' cannot be used to satisfy nonisolated protocol requirement.

(Confusingly, if I look at the generated interface for UIListContentView, its configuration property has a @MainActor attribute.)

What am I missing here? How could these types implement these protocols while adhering to the strict concurrency rules?

Upvotes: 5

Views: 513

Answers (1)

Sixten Otto
Sixten Otto

Reputation: 14816

Follow up: it appears that in the beta iOS 17 SDK, the UIContentView protocol, its members, and the method UIContentConfiguration.makeConfiguration() have all been marked with the @MainActor attribute.

Upvotes: 1

Related Questions