esbenr
esbenr

Reputation: 1518

Updating to XCode 16.2 produces a lot of concurrency warnings

My pattern for fetching stuff from my API is like this in a ObservableObject class:

@Published var featureToggles: [FeatureToggle] = []

func fetchFeatureToggles() async -> Void {
        let result: Result<[String], OperatorAppError> = await networkController.getUrlDataResult(.featureToggles)
        switch result {
        case .success(let fts):
            DispatchQueue.main.async {
                self.featureToggles = fts.compactMap { FeatureToggle(rawValue:$0) }
                print("Feature toggles fetched: \(self.featureToggles)")
            }
        case .failure(let error):
            errorHandler.handleError(title: "Error while fetching feature toggles", error: error)
        }
    }

After updating from Xcode 16.1 -> Xcode 16.2 the DispatchQueue.main.async {} is causing a lot of concurrency warnings. Originally I did this to allow the fetch to happen in an async task and only update the @Published property in sync with the main thread.

Removing the main thread safety and adding @MainActor removes the warnings, but what happens and what are the concequences?

@Published var featureToggles: [FeatureToggle] = []

@MainActor
    func fetchFeatureToggles() async -> Void {
        let result: Result<[String], OperatorAppError> = await networkController.getUrlDataResult(.featureToggles)
        switch result {
        case .success(let fts):
            self.featureToggles = fts.compactMap { FeatureToggle(rawValue:$0) }
            print("Feature toggles fetched: \(self.featureToggles)")
        case .failure(let error):
            errorHandler.handleError(title: "Error while fetching feature toggles", error: error)
        }
    }

Will the fetch still run as an async task even though it is marked @MainActor?

Upvotes: 0

Views: 183

Answers (0)

Related Questions