I have the following code:
import SwiftUI
import Observation
struct ContentView: View {
var body: some View {
VStack {
Image(systemName: "globe")
.imageScale(.large)
.foregroundStyle(.tint)
Text("Hello, world!")
}
.padding()
}
}
@Observable class ViewModel {
struct TestInfo {
var myId:Int
}
@MainActor var info:TestInfo?
func setupInfo() async {
var tmp:TestInfo?
await CoreData.shared.context.perform {
/// some core data work and then we set tmp
tmp = TestInfo(myId: ....)
}
await MainActor.run {
info = tmp /// ERROR: Reference to captured var 'tmp' in concurrently-executing code
}
}
}
I would like to know how to fix this error. I setup tmp
within the core data context closure and would like to then set info
to tmp
.