How to resolve SwiftUI.DynamicProperty on MainActor compiler warning on 6.0?

Hi! I'm running into a warning from a SwiftUI.DynamicProperty on a 6.0 development build (swift-6.0-DEVELOPMENT-SNAPSHOT-2024-03-26-a).

I am attempting to build a type (conforming to DynamicProperty) that should also be MainActor. This type with also need a custom update function. Here is a simple custom wrapper (handwaving over the orthogonal missing pieces) that shows the warning:

import SwiftUI

@MainActor struct MainProperty: DynamicProperty {
  //  Main actor-isolated instance method 'update()' cannot be used to satisfy nonisolated protocol requirement; this is an error in the Swift 6 language mode
  @MainActor func update() {
    
  }
}

Is there anything I can do about that warning? Does the warning correctly imply that this will be a legit compiler error when 6.0 ships?

I can find (at least) two examples of types adopting DynamicProperty from Apple that are also MainActor: FetchRequest and SectionedFetchRequest. What is confusing is that both FetchRequest^1 and SectionedFetchRequest^2 explicitly declare their update method to be MainActor. Is there anything missing from my Wrapper declaration that can get me what I'm looking for? Any more advice about that? Thanks!

If you’re working with Swift development toolchains, it’s best to discuss any issues you encounter on Swift Forums. DevForums tries to stay focused on the stuff that Apple has shipped (or seeded as betas).

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

@eskimo Sounds good. Thanks!

I'm seeing this same error from Xcode_16_beta_3. Is there anything that can be communicated to engineers about the future direction of DynamicProperty? It seems like the type of work that could be expected to be on main… is the explicit MainActor requirement on DynamicProperty coming later before the new OS goes to production?

I had the same error and fixed it by adding an extra @preconcurrency to the protocol conformance, e.g.

... : @preconcurrency DynamicProperty {

Mine actually looks like this though:

@MainActor @propertyWrapper @preconcurrency public struct DynamicQuery<ResultType>: @preconcurrency DynamicProperty where ResultType: PersistentModel {

I noticed that @FetchRequest has did it on a protocol extension, maybe that was their trick:

extension FetchRequest : DynamicProperty {
    @MainActor @preconcurrency public mutating func update()
}

I wonder why their update func is mutating, that must be a mistake surely. The behaviour of @FetchRequest is rather strange, e.g. when re-init it loses any dynamically configured properties. I have had to implement my own to fix it, it's here: https://github.com/malhal/SwiftUICoreDataFetchRequestRedesign

How to resolve SwiftUI.DynamicProperty on MainActor compiler warning on 6.0?
 
 
Q