Friday, June 21, 2024
HomeiOS DevelopmentReferenced to captured var in concurrently-execute code?

Referenced to captured var in concurrently-execute code?


I’ve the next code:

import SwiftUI
import Statement

struct ContentView: View {

    @State var vm = ViewModel()
    
    var physique: some View {
        VStack {
            Picture(systemName: "globe")
                .imageScale(.giant)
                .foregroundStyle(.tint)
            Textual content("Hiya, world!")
        }
        .padding()
    }
    
}

@Observable class ViewModel {
    
    struct TestInfo {
        var myId:Int
    }
    
    @MainActor var data:TestInfo?
    
    func setupInfo() async {
        
        var tmp:TestInfo?
        
        await CoreData.shared.context.carry out {

            /// some core knowledge work after which we set tmp
            tmp = TestInfo(myId: ....)
        }
        
        
        await MainActor.run {
            data = tmp /// ERROR: Reference to captured var 'tmp' in concurrently-executing code
        }
        
    }
    
}

I want to know tips on how to repair this error. I setup tmp inside the core knowledge context closure and want to then set data to tmp.



Supply hyperlink

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments