I'm trying to use a SwiftUI view as UICalendarView
decoration and I get Call to main actor-isolated instance method 'makeContentView()' in a synchronous nonisolated context; this is an error in the Swift 6 language mode in the following code:
class Coordinator: NSObject, UICalendarViewDelegate {
func calendarView(_ calendarView: UICalendarView, decorationFor dateComponents: DateComponents) -> UICalendarView.Decoration? {
.customView {
UIHostingConfiguration {
...
}
.makeContentView()
}
}
}
I've fixed using MainActor.assumeIsolated
but is this the correct approach or is there a better one?
class Coordinator: NSObject, UICalendarViewDelegate {
func calendarView(_ calendarView: UICalendarView, decorationFor dateComponents: DateComponents) -> UICalendarView.Decoration? {
.customView {
MainActor.assumeIsolated {
UIHostingConfiguration {
...
}
.makeContentView()
}
}
}
}
There are a number of different approaches but this is a perfectly reasonable one. It assumes that the calendar view calls its delegate on the main thread / queue / actor, which is a reasonable assumption for a view class in the absence of any documentation to the contrary.
Feel free to file a bug against the calendar view requesting that it delegate be decorated so that the Swift compiler understands this. And please post your bug number, just for the record.
Migrating to Swift 6 is chock-full of useful info in this space.
Share and Enjoy
—
Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"