tcldr
July 20, 2024, 11:20am
58
ConfusedVorlon:
Class is constructed in the background alongside some potentially expensive file access. Indeed -that's what I'm doing in my case. A bunch of files have to be loaded, ML analysed in order to construct the class.
I could do that work on the background - then hop back to main to initialise, but that doesn't gain me anything.
Then it's passed to the @MainActor so that it can be edited and displayed
Maybe region based isolation plus transferring
closures will be the ointment here. You'll still need to remove the @MainActor
annotations on the non isolated protocol conformances, however.
This post gives a good summary:
As a quick example of this in action, consider the following:
class NonSendable { }
extension Task where Failure == Never {
// Modified Task.init() converting it to a non-Sendable transferring implementation.
init(x: (), @_inheritActorContext @_implicitSelfCapture operation: transferring @escaping () async -> Success) {
/* ... */
}
}
func spawn() {
let ns = NonSendable()
Task.init(x: ()) {
_ = ns
}
}
IIUC, this should allow you to construct your @MainActor
destined class off the main actor, then pass it to the @MainActor
once constructed.
2 Likes