Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Task runner listeners need to be called on main thread #142

Open
levibostian opened this issue Apr 6, 2024 · 0 comments
Open

Task runner listeners need to be called on main thread #142

levibostian opened this issue Apr 6, 2024 · 0 comments
Labels
enhancement New feature or request

Comments

@levibostian
Copy link
Owner

Given this SwiftUI code:

struct ContentView: View {
    
    @ObservedObject var viewModel = ViewModel()
    
    var body: some View {
		...
    }
    
    class ViewModel: ObservableObject, TaskRunnerListener {
        @Published var taskText: String = "Hello, World"
        
        init() {
            WendyConfig.addTaskRunnerListener(self)
        }
        
        func runningTask(_ task: PendingTask) {
            self.taskText = "Running task..."
        }
        func allTasksComplete() {
            self.taskText = "All tasks complete!"
        }
    }
}

If I run this code, I will get runtime errors saying that I should be updating the self.taskText value on the main thread, not background.

I resolve these errors if I do:

    class ViewModel: ObservableObject, TaskRunnerListener {        
        func runningTask(_ task: PendingTask) {
			Task { @MainActor in 
              self.taskText = "Running task..."
            } 
        }
    }

All listeners should be called on main actor.

@github-project-automation github-project-automation bot moved this to Backlog/Refinement in Wendy-iOS Apr 6, 2024
@levibostian levibostian added the enhancement New feature or request label Apr 6, 2024
@levibostian levibostian added this to the Joy ♥️😄 milestone Apr 9, 2024
@levibostian levibostian moved this from Backlog/Refinement to Ready in Wendy-iOS May 26, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
Status: Ready
Development

No branches or pull requests

1 participant