Created
February 12, 2026 22:41
-
-
Save WFT/fdd01a16d2a03a3e2bfca2978d6a79d5 to your computer and use it in GitHub Desktop.
Nonisolated Init
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import Dispatch | |
| let someDispatchQueue = DispatchQueue() | |
| @MainActor | |
| class Foo { | |
| nonisolated static let shared = Foo() | |
| var mybar: Bar // I only need to get this from the main actor | |
| // In the real code there are many properties; that's why I'm not tagging | |
| // each one @MainActor on its own, but it should boil down to the same thing. | |
| nonisolated init() { | |
| mybar = getCachedBar() // Warning: Main actor-isolated property 'mybar' can not be mutated from a nonisolated context; this is an error in the Swift 6 language mode | |
| } | |
| nonisolated func updateBar() { | |
| someDispatchQueue.async { | |
| let newBar = createNewBarOMGItTakesSoLong() | |
| DispatchQueue.main.async { | |
| self.mybar = newBar | |
| } | |
| } | |
| } | |
| } | |
| class Bar { | |
| var i = 0 | |
| } // Nonsendable | |
| func createNewBarOMGItTakesSoLong() -> sending Bar { | |
| let bar = Bar() | |
| for i in 0..<1000 { bar.i += i } | |
| return bar | |
| } | |
| func getCachedBar() -> sending Bar { | |
| Bar() | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment